Extracting Ajax return data in jQuery

后端 未结 6 1007
臣服心动
臣服心动 2020-11-29 01:36

I have done jQuery and Ajax, but I am not able to get the response into a Div element. This is the code:

Index.html

$.ajax({
    typ         


        
相关标签:
6条回答
  • 2020-11-29 01:55

    You can use json like the following example.

    PHP code:

    echo json_encode($array);
    

    $array is array data, and the jQuery code is:

    $.get("period/education/ajaxschoollist.php?schoolid="+schoolid, function(responseTxt, statusTxt, xhr){
        var a = JSON.parse(responseTxt);
        $("#hideschoolid").val(a.schoolid);
        $("#section_id").val(a.section_id);
        $("#schoolname").val(a.schoolname);
        $("#country_id").val(a.country_id);
        $("#state_id").val(a.state_id);
    }
    
    0 讨论(0)
  • 2020-11-29 01:56

    You may also use the jQuery context parameter. Link to docs

    Selector Context

    By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function

    Therefore you could also have:

    success: function(data){
        var oneval = $('#one',data).text();
        var subval = $('#sub',data).text();
    }
    
    0 讨论(0)
  • 2020-11-29 01:58

    I have noticed that your success function has the parameter "html", and you are trying to add "data" to your elements html()... Change it so these both match:

    $.ajax({
        type:"POST",
        url: "ajax.php",
        data:"id="+id ,
        success: function(data){
            $("#response").html(data);
        }
    });
    
    0 讨论(0)
  • 2020-11-29 01:59

    Change the .find to .filter...

    0 讨论(0)
  • 2020-11-29 02:12

    You can use .filter on a jQuery object that was created from the response:

    success: function(data){
        //Create jQuery object from the response HTML.
        var $response=$(data);
    
        //Query the jQuery object for the values
        var oneval = $response.filter('#one').text();
        var subval = $response.filter('#sub').text();
    }
    
    0 讨论(0)
  • 2020-11-29 02:15
    on success: function (response) { alert(response.d); }
    
    0 讨论(0)
提交回复
热议问题