0

I have a simple ajax post to the server..

$(".invite-team-members-submit-btn").click(function() {
  $.post("invite_team_member", { 
    token: $("#token").val(), 
    email: $("#email").val(), 
    team: $("#team").val() 
  },"json")
    .done(function (responseText) {
      alert(responseText.response);
    })
    .fail(function (xhr,status,message) { alert("ERROR: " + message); })
    .then(function () { alert("Something should happen."); });
});

The JSON returned looks like this...

{"response":"Person has been invited."}

My response header in the console looks like this...

Response Headers
  Cache-Control max-age=0, private, must-revalidate
  Connection    close
  Content-Type  application/json; charset=utf-8
  Date  Wed, 22 May 2013 21:45:07 GMT
  Etag  "e5b5e12acbcc78372b2a861027b66c05"
  Status    200 OK
  Transfer-Encoding chunked
  X-Request-Id  d835ce021eff7733d67ebfcdd468bdf2
  X-Runtime 0.007909
  x-ua-compatible   IE=Edge

My console is not only telling me that I received a 200 status but that my response includes the JSON I want. However when it comes to handling the parsed JSON no alerts are made. Note that I'm receiving NO alerts, not even from the then function. What could be causing this?

3
  • console.log(responseText) and console.log(typeof responseText) Since you are returning the application/json content-type header, jQuery may be parsing it for you, where it then later errors on jQuery.parseJSON because you're trying to parse an object rather than a string. Commented May 22, 2013 at 22:01
  • What @KevinB said; if you are already returning JSON, don't use parseJSON Commented May 22, 2013 at 22:02
  • Nothing was returned in the console log. It acted as if the server was never hit, unless the console is playing tricks with me. Commented May 22, 2013 at 22:05

1 Answer 1

1

This is most likely due to jQuery automatically parsing the json for you before the success callback. To cause this to always happen so that you can consistently expect it, use this:

$(".invite-team-members-submit-btn").click(function() {
  $.post("invite_team_member", { 
    token: $("#token").val(), 
    email: $("#email").val(), 
    team: $("#team").val() 
  },"json")
    .done(function (responseText) {
      alert(responseText.response);
    })
    .fail(function (xhr,status,message) { alert("ERROR: " + message); })
    .then(function () { alert("Something should happen."); });
});

note the "json" parameter at the end of line 2

Update There was a ; at the end of $.post() making .done, .fail, and .then invalid. fixed in the above code.

If you still aren't seeing either a done alert or a fail alert, then there's an error in your console or the click event isn't happening in the first place. If using Firefox, make sure you're using firebug too. If testing in IE, try another browser.

Sign up to request clarification or add additional context in comments.

4 Comments

No such luck. I didn't get an alert. I posted your answer to match my question to keep things up to date.
See the latest addition, if there's no alert for done or fail, you've got a js error in the console, or the click event isn't happening, or the ajax is being aborted due to a form submit or page refresh.
Shouldn't you be returning false from this event?
Sigh, I should have double checked the class name selector. Your last comment run-down helped me find the fix. Thanks to Explosion Pills also for helping out in a previous question related to this.

Your Answer

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.