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

时间:2022-12-08 19:43:26

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?

有人有什么主意吗?

3 个解决方案

#1


14  

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(数据);在您的成功函数中查看是否有任何返回。

You could also use .always(data):

你也可以使用.always(数据):

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.

弃用注意:jQuery 1.8中不推荐使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。要准备最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

#2


4  

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. 假设您期望一个JSON对象(您使用“Accept”mime类型请求它)。
  3. Suppose the Json string is not valid.
  4. 假设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()中,您将获得“纯粹”格式的答案作为纯文本。

#3


1  

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函数,您将找到这些属性。

#1


14  

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(数据);在您的成功函数中查看是否有任何返回。

You could also use .always(data):

你也可以使用.always(数据):

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.

弃用注意:jQuery 1.8中不推荐使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。要准备最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

#2


4  

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. 假设您期望一个JSON对象(您使用“Accept”mime类型请求它)。
  3. Suppose the Json string is not valid.
  4. 假设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()中,您将获得“纯粹”格式的答案作为纯文本。

#3


1  

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函数,您将找到这些属性。