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?
-
You have to parse the JSON into a JavaScript object: stackoverflow.com/questions/4935632/…Felix Kling– Felix Kling2011年09月07日 18:07:04 +00:00Commented Sep 7, 2011 at 18:07
4 Answers 4
Like so:
alert(response.success);
answered Sep 7, 2011 at 18:06
Naftali
147k41 gold badges248 silver badges304 bronze badges
$.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.');
}
});
answered Sep 7, 2011 at 18:06
genesis
51.1k20 gold badges99 silver badges127 bronze badges
Comments
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.
1 Comment
Linda
thanks, adding the dataType did it. I called it right before posting he question but the dataType was required for me.
Try this:
alert(response.success);
answered Sep 7, 2011 at 18:05
Chandu
83.2k19 gold badges137 silver badges137 bronze badges
Comments
lang-js