如何访问JQuery AJAX请求的完整功能中返回的JSON中存储的数据。例如,我有以下代码:
$.ajax({
url: 'buildings.php',
data: "building=" + building,
complete: function (response) {
alert(response.responseText);
alert(response.name);
}
});
在第一个警报中,它显示以下内容,这是我从PHP发送的预期JSON数据。
{"name":"HSB","description":"description","directionsURL":"directionsURL","imageArray":{"1":"URL 1","2":"URL 2"}}
在第二个警报中,它显示
undefined
如何访问在第一个警报中显示的接收到的数据?
如果添加dataType: "json"
到呼叫中,则将使响应成为json对象:
$.ajax({
url: 'buildings.php',
data: "building=" + building,
dataType: "json",
complete: function (response) {
alert(response.name);
}
});
编辑:因此,无论出于何种原因,jQuery都无法自动解析它,但是JSON.parse(response.responseText)
成功了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句