Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback not executing from jQuery post

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?

like image 764
jacekn Avatar asked May 26 '12 16:05

jacekn


2 Answers

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.

like image 97
T.J. Crowder Avatar answered Sep 30 '22 07:09

T.J. Crowder


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");
}
like image 45
Constant Meiring Avatar answered Sep 30 '22 07:09

Constant Meiring