7

I'm having problems executing the call back function.

$.post("/" + contentId + "/postComment", {
    "postComment": ""
}, function(data) {
    alert('call back');
});

This post does take place. The alert is not called, however.

This post results in some xml returned. I can't tell exactly how it looks because I'm using Spring mappings of application/xml together with @RequestBody and I just don't know what Spring does to what I'm returning. I'm saying this just in case the contents of server response can affect the call back somehow.

The question is:

what do I need to do to see that alert in my code example?

Community
  • 1
  • 1
jacekn
  • 1,521
  • 5
  • 29
  • 50

4 Answers4

26

Your code is fine other than that it doesn't hook the error handler (one of the reasons I don't like $.post). I think the POST operation must be resulting in an error. Try converting it to this:

$.ajax({
  type:    "POST",
  url:     "/"+contentId+"/postComment",
  data:    {"postComment":""},
  success: function(data) {
        alert('call back');
  },
  // vvv---- This is the new bit
  error:   function(jqXHR, textStatus, errorThrown) {
        alert("Error, status = " + textStatus + ", " +
              "error thrown: " + errorThrown
        );
  }
});

...so you can see what the error is.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    +1 because using $.ajax is almost always preferable for exactly the reason you specified. – Josh Hudnall May 26 '12 at 16:58
  • Well, it is alerting 'Error'. Can we get to details of the error, somehow? – jacekn May 26 '12 at 17:00
  • @jacekn: You may have seen an earlier copy of the answer; refresh, there's more information available (or refer to the [`ajax` docs](http://api.jquery.com/jQuery.ajax/)). But the best information would come from the browser's debugger. If you're using any modern, quality browser it either has quite a good debugger built in (with network operations listed, including content) or available as an add-on (Firebug, in the case of Firefox). Chrome, Safari, and Opera all have very good ones built in. IE8 and higher have acceptable ones. – T.J. Crowder May 26 '12 at 17:04
  • OK, I see it. Error details give me important info. Thank you, sir! – jacekn May 26 '12 at 17:06
6

Had a similar issue where the callback doesn't fire when you provide a string as data and the server returns a JSON response. To get around this, simply explicitly specify the datatype to be JSON:

function update_qty(variant_id, qty){
  $.post('/cart/update.js', "updates["+variant_id+"]="+qty, function(data){
    updateCartDesc(data);
  }, "json");
}
Constant Meiring
  • 3,285
  • 3
  • 40
  • 52
1

For readability, I would recommend to ignore the above correct answer. As of JQuery 1.5 (or later, and in the questions case) use:

$.post("/" + contentId + "/postComment", {
    "postComment": ""
}).always(function() {
    alert('call back');
});

as stated in the JQuery documentation: https://api.jquery.com/jquery.post/
It's not very clear from the documentation but you get the returned "jqXHR" object from the post request as parameter to the always, fail, and done functions.

You can extend the parameters, as you need, up to:

.always(function(responseArray, request, jqxhr, status, error) {
    alert( jqxhr.responseText );
});
AgathoSAreS
  • 378
  • 3
  • 7
0

I noticed that the ajax script needs to have a content-type of application/json rather than application/javascript, if it is indeed JSON.

dtbarne
  • 8,110
  • 5
  • 43
  • 49
  • 1
    No, if its a script, its type is application/javascript. application/json is for json. hacking the mime types just undermines the browser document model – Shayne Nov 18 '16 at 03:59
  • @Shayne nobody said it was a script. Come down from that pedestal. :) – dtbarne Nov 28 '16 at 19:53
  • 1
    Well now that you edited your post, sure. But prior to that its pretty much literally what you had said. – Shayne Dec 02 '16 at 01:55