javascript - Using a JSON response - Stack Overflow

I get the following response from the server after doing an ajax request:{"error":false,"

I get the following response from the server after doing an ajax request:

{"error":false,"success":true}

My ajax code:

$.ajax({
    url: '/update',
    type: 'post',
    data: $(this).serialize(),
    success: function(response) {
        alert(response)
    },
    error: function() {
        alert('An error occured, form not submitted.');
    }
});

instead of alerting the whole response I want to alert the value of "success", which in this case would be true. How to do this?

I get the following response from the server after doing an ajax request:

{"error":false,"success":true}

My ajax code:

$.ajax({
    url: '/update',
    type: 'post',
    data: $(this).serialize(),
    success: function(response) {
        alert(response)
    },
    error: function() {
        alert('An error occured, form not submitted.');
    }
});

instead of alerting the whole response I want to alert the value of "success", which in this case would be true. How to do this?

Share Improve this question edited Oct 29, 2011 at 2:35 Clive 37k8 gold badges89 silver badges113 bronze badges asked Sep 7, 2011 at 18:04 LindaLinda 1431 gold badge1 silver badge4 bronze badges 1
  • You have to parse the JSON into a JavaScript object: stackoverflow./questions/4935632/… – Felix Kling Commented Sep 7, 2011 at 18:07
Add a ment  | 

4 Answers 4

Reset to default 4

Like so:

alert(response.success);
   $.ajax({
        url: '/update',
        type: 'post',
        dataType: 'json', 
        data: $(this).serialize(),
        success: function(response) {

                        alert(response.success)

        },
        error: function() {
            alert('An error occured, form not submitted.');
        }
    });
alert(response.success);

would do it, you can add dataType: 'json' to your $.ajax options to make absolutely sure it's evaluated as an object in your callback.

Try this:

alert(response.success);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745652673a4638357.html

相关推荐

  • javascript - Using a JSON response - Stack Overflow

    I get the following response from the server after doing an ajax request:{"error":false,"

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信