开发者

Using a JSON response

开发者 https://www.devze.com 2023-04-03 02:27 出处:网络
I get the following response from the server after doing an ajax request: {\"error\":false,\"success\":true}

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 开发者_JAVA技巧alert the value of "success", which in this case would be true. How to do this?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消