【发布时间】:2013-09-15 11:05:26
【问题描述】:
我是 Jquery 和 Ajax 的新手。我已经编写了以下代码,在单击表单组合框中的选项后自动填充表单,通过这篇文章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);
}
});
});
但是所有这些值 student.email, student.roll_num 都变成空白,当我发出声明 alert(student) 时,它正在像这样打印 [object object]。但是,当我在浏览器中调用相同的 json 调用时,我得到了学生的预期 json 对象,在其中我得到了所有学生属性的正确值。我在上面的代码中的任何地方都做错了吗?因此,如果有人帮助我解决此问题,我将不胜感激。谢谢。
【问题讨论】:
-
你能发布 /students_database 行动声明吗?
标签: javascript jquery ruby-on-rails ajax json