3

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?

asked Sep 7, 2011 at 18:04
1

4 Answers 4

4

Like so:

alert(response.success);
answered Sep 7, 2011 at 18:06
Sign up to request clarification or add additional context in comments.

2 Comments

@genesis -- yes it will, $.ajax() usually knows what type the return is based on the response
in case Content-Type is application/json. +1 though
3
 $.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

Comments

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

answered Sep 7, 2011 at 18:08

1 Comment

thanks, adding the dataType did it. I called it right before posting he question but the dataType was required for me.
2

Try this:

alert(response.success);
answered Sep 7, 2011 at 18:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.