我有一个这样的对象,我通过 AJAX 请求检索它:
{
"id": "1",
"name": "Eyes"
}
如何获取文本字段 #id_attributes
和 #name_attributes
的数据?单击按钮时我尝试了此操作,但它给了我未定义/空白?
$.ajax({
url: "to my json",
cache: false,
type: "POST",
data: 'id=' + id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
谁能帮帮我?
最佳答案
尝试:
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json'
data:{id:id},
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
或者解析结果
success: function (result) {
var jsonresult = JSON.parse(result);
$("#id_attributes").val(jsonresult.id);
$("#name_attributes").val(jsonresult.name);
}
关于php - 从通过 AJAX 检索的对象设置文本字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34196453/