admin管理员组文章数量:1404337
$.ajax({
type: 'POST',
url: 'place/add',
data: {
lat: lat,
lng: lng,
name: name,
address: address,
phone: phone,
review: review,
category: category
},
success: function(data) {
alert(data);
alert(data.id);
// ......
});
The first alert gives:{"id":"2","success":true}
, but the second: undefined
$.ajax({
type: 'POST',
url: 'place/add',
data: {
lat: lat,
lng: lng,
name: name,
address: address,
phone: phone,
review: review,
category: category
},
success: function(data) {
alert(data);
alert(data.id);
// ......
});
The first alert gives:{"id":"2","success":true}
, but the second: undefined
3 Answers
Reset to default 11You need to specify your anticipated returned data type as JSON:
$.ajax({
type: 'POST',
dataType: 'json', // specifies the return type
url: 'place/add',
data: {
lat: lat,
lng: lng,
name: name,
address: address,
phone: phone,
review: review,
category: category
},
success: function(data) {
alert(data);
alert(data.id);
// ......
}
});
An especially useful addition, if you run multiple ajax calls is $.ajaxSetup
$.ajaxSetup({
type: 'post',
dataType: 'json'
});
Any subsequent ajax calls will use these as the defaults.
You have to specify dataType: 'json'
or eval returned data yourself like this var data = eval('(function(){return '+data+'})()');
BTW trust jQuery - use dataType: 'json'
if you can.
本文标签: javascriptDebugging jQuery AJAX response what I39m doing wrongStack Overflow
版权声明:本文标题:javascript - Debugging jQuery AJAX response: what I'm doing wrong? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744783268a2624843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论