javascript - Debugging jQuery AJAX response: what I'm doing wrong? - Stack Overflow

$.ajax({type: 'POST',url: 'placeadd',data: {lat: lat,lng: lng,name: name,address:

$.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

Share Improve this question edited Mar 1, 2019 at 14:52 user3307073 asked Nov 8, 2009 at 12:36 DerkDerk 1812 gold badges2 silver badges6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 11

You 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信