$.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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744783268a4593470.html
评论列表(0条)