简体   繁体   English

使用Jquery解析Json格式的Ajax响应

[英]Parsing ajax response in Json format with Jquery

I am new to Jquery and Ajax. 我是Jquery和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 : 我编写了以下代码,以通过根据基于Jquery和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. 但是所有这些值student.email, student.roll_numstudent.email, student.roll_num空白,并且当我发出语句alert(student) ,它正在像这样打印[object object] 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. 但是,当我在浏览器中调用相同的json调用时,我得到了该学生的预期json对象,在该对象中我获得了所有Student属性的正确值。 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 这里U =页面的网址,F =页面的功能

$("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;
            }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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