Callback not executed from jQuery message

I'm having problems executing a callback function.

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

This message is taking place. However, a warning is not triggered.

As a result of this message some xml returned. I can’t say exactly what this looks like, because I use Spring application/xml @RequestBody along with @RequestBody , and I just don’t know what Spring does for it to be returned. I say this just in case the contents of the server response can somehow affect the callback.

Question:

What do I need to do to see this warning in my sample code?

+4
source share
3 answers

Your code is in order, different from the fact that it does not include an error handler (one of the reasons I don't like $.post ). I think the POST operation should result 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 a mistake is.

+21
source

Had a similar problem when the callback does not work, when you provide a string as data, and the server returns a JSON response. To get around this, simply explicitly specify the data type as JSON:

 function update_qty(variant_id, qty){ $.post('/cart/update.js', "updates["+variant_id+"]="+qty, function(data){ updateCartDesc(data); }, "json"); } 
+1
source

I noticed that the ajax script should have the content type application/json , not application/javascript , if it is really JSON.

0
source

Source: https://habr.com/ru/post/1414532/


All Articles