jQuery 为什么jquery ajax回调函数不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10422244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:19:56  来源:igfitidea点击:

Why does jquery ajax callback function not work?

jqueryajaxcallbackjquery-callback

提问by dchacke

I am having a problem with a simple callback function in jQuery ajax. Google won't help and stack overflow wouldn't either, so I guess this might not be something specific but rather something I am too ignorant to see. To me the code looks exactly like it should.

我在 jQuery ajax 中遇到了一个简单的回调函数的问题。谷歌不会提供帮助,堆栈溢出也不会,所以我想这可能不是特定的东西,而是我太无知而无法看到的东西。对我来说,代码看起来完全像它应该的那样。

So, here's the code:

所以,这是代码:

function sendMessage(message)
{
//Establish connection to php script
$.ajax({
    type: 'POST',
    url: 'action/chat/test.php',
    success: function(feedback){

        alert(feedback);

    }
}).error(function(){
    //Do some error handling here
});
}

In test.php it simply says

在 test.php 中它只是说

<?php
    echo "called";
?>

As far as I am concerned "called" should be alerted - but it isn't. I have checked already that the function sendMessage() is called (and the parameter message doesn't matter for now).

就我而言,应该提醒“被叫”——但事实并非如此。我已经检查过函数 sendMessage() 是否被调用(并且参数消息现在无关紧要)。

Does anyone have any idea?

有谁有想法吗?

回答by Fostah

Update:One thing to note also, make sure you use some kind of debugger like firebug. Then you can go to the network tab and look at the request url and response manually to see if its getting a 200 response or internal server error, etc.

更新:还要注意一件事,确保您使用某种调试器,如 firebug。然后您可以转到网络选项卡并手动查看请求 url 和响应,以查看它是否收到 200 响应或内部服务器错误等。

Try adding a console.log(data);in your success function to see if anything is being returned.

尝试console.log(data);在您的成功函数中添加 a以查看是否有任何返回。

You could also use .always(data):

您还可以使用.always(data)

function sendMessage(message)
{
  //Establish connection to php script
  $.ajax({
      type: 'POST',
      url: 'action/chat/test.php'   
  }).done(function(data) { console.log(data); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });
}

From the docs:

从文档:

Deprecation Notice:The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调将在 jQuery 1.8 中弃用。要为最终删除代码做好准备,请改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

回答by Nadir

Just for the reference, there is a behavior that may end up like this (ie done() not called).

仅供参考,有一种行为可能会像这样结束(即 done() 未调用)。

Here it is:

这里是:

  1. Suppose you expect a JSON object (you asked for it with the "Accept" mime type).
  2. Suppose the Json string is not valid.
  1. 假设您期望一个 JSON 对象(您使用“Accept”mime 类型请求它)。
  2. 假设 Json 字符串无效。

In this case done() is never called, but always() will be. And in always() you will get the "badly" formatted answer as pure text.

在这种情况下,done() 永远不会被调用,但 always() 会被调用。而在 always() 中,您将获得“严重”格式的答案作为纯文本。

回答by DanRedux

It's probably the error handling. If you want to handle errors, there's another attribute you can give the object you pass in to ajax, it's "error", and also "timeout" for handling page timeouts. Look up the $.ajax function and you'll find these attributes.

这可能是错误处理。如果你想处理错误,还有另一个属性你可以给你传递给ajax的对象,它是“错误”,还有用于处理页面超时的“超时”。查找 $.ajax 函数,您会发现这些属性。