简体   繁体   中英

Parsing ajax response in Json format with Jquery

I am new to Jquery and Ajax. I have written the following code to autopopulate a form after clicking a option in a combo box in a form by taking help from this post Autopopulate form based on selected value from combo box with Jquery and Ajax :

 $("select#student_id").change(function(){
    var student_id = $(this).val(); //Here I am getting the selected value from 
    //the combo box
    $.ajax({
        url: "/students.json?student_id="+student_id, //this is the url that will
        //send me one student object
        dataType: "json",
        success: function(student) {
           $('#student_email').val(student.email);
           $('#student_roll_num').val(student.roll_num);
        }    
    });
 });

But all these values student.email, student.roll_num are coming blank and when I am issuing the statement alert(student) , it is printing [object object] like that. But when I am invoking the same json call in the browser I get expected json object of the student where I get correct values for all student attributes. Am I doing wrong anywhere in this above code? So if anybody helps me to fix this, I will be really grateful. Thank you.

Try this, this is help you. Here U = Url of Page and F = Function of Page

$("select#student_id").change(function(){
        var student_id = $(this).val();   
    $.ajax({
            type: "POST",
            url: U + '/' + F,
            data: "{id: " + student_id + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (response) {
                var content=response.d;
            }
        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM