English 中文(简体)
从 jQuery 职位调回未执行的回调
原标题:Callback not executing from jQuery post

我在执行回电功能时遇到问题。

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

这个职位确实存在,但是没有发出警报。

此文章的结果是返回了某些 < code> xml 。 我无法确切辨别它的外观, 因为使用 < code > application/xml 的 < code > 应用程序/ xml < /code > 的 < pring > / strong > 映射和 < code> < translateBody , 我实在不知道 Spring 对返回的我做了什么。 我这样说, 以防服务器响应的内容会影响回调 。

问题在于:

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

最佳回答

您的代码是罚款的, 只是它不会勾勾错误处理器( 我不喜欢 $. post 的原因之一) 。 我认为 POST 操作必须导致错误 。 请尝试将其转换为 :

$.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
        );
  }
});

...你可以看到错误是什么

问题回答

当您将字符串作为数据提供时,回调不开火,而服务器则返回 JSON 响应。要绕过这个问题,只需明确指定数据类型为 JSON :

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

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.

您可以根据需要将参数扩展至:

.always(function(responseArray, request, jqxhr, status, error) {
    alert( jqxhr.responseText );
});

我注意到,ajax脚本的内容类型必须是application/json ,而不是application/javascript ,如果确实是JSON的话。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签