$ .ajax()成功不会运行函数

时间:2022-10-02 21:11:58

My question regards the $.ajax() jQuery method. I can't get the success parameter in $.ajax() to work.

我的问题是关于$ .ajax()jQuery方法。我无法在$ .ajax()中获取成功参数。

This works:

$.ajax({
    type: 'POST',
    url: "/getCodes.php?codes=billingCodes&parent="+$('#wClient').val(),
    dataType: 'json',
    success: window.alert("inside aJax statement")

});

This does not:

这不是:

 $.ajax({
    type: 'POST',
    url: "/getCodes.php?codes=billingCodes&parent="+$('#wClient').val(),
    dataType: 'json',
    success: function(){
        window.alert("inside aJax statement");
    }                       
}); 

In the first case, I get a JavaScript alert window that lets me know the $.ajax() I called is working. All that is changed in the second block of code is I put the window.alert() inside a function() { window.alert(); }.

在第一种情况下,我得到一个JavaScript警报窗口,让我知道我调用的$ .ajax()正在工作。在第二个代码块中改变的是我把window.alert()放在一个function(){window.alert(); }。

The point of this is to verify that the $.ajax is running so I can put some actual useful code in the function(){} when the $.ajax runs successfully.

这一点的目的是验证$ .ajax是否正在运行,这样当$ .ajax成功运行时,我可以在function(){}中放入一些实际有用的代码。

6 个解决方案

#1


5  

In your second example nothing will happen unless you get a successful call back from the server. Add an error callback as many here have suggested to see that indeed the ajax request is working but the server is not currently sending a valid response.

在您的第二个示例中,除非您从服务器成功回拨,否则不会发生任何事情。添加一个错误回调,正如许多人建议的那样,确实ajax请求确实有效,但服务器当前没有发送有效的响应。

$.ajax({
        type: "POST",
        url: "/getCodes.php?codes=billingCodes&parent="+$('#wClient').val(),                        
        dataType:"json",
        success: function(response){
                alert(response);        
      },
      error: function(jqXHR, textStatus, errorThrown){
          alert('error');
      }         
      });

helpful Link in tracking down errors.

有用的链接跟踪错误。

#2


2  

Your first example does nothing whatsoever to prove that the ajax call has worked. All it does is prove that the ajax function was reached, because the values of the properties in the anonymous object you're passing into the ajax function are evaluated before the function is called.

你的第一个例子没有做任何事情来证明ajax调用有效。所有这一切都证明了ajax函数已经达到,因为在调用函数之前会评估您传递给ajax函数的匿名对象中的属性值。

Your first example is basically the same as this:

你的第一个例子与此基本相同:

// THIS IS NOT A CORRECTION, IT'S AN ILLUSTRATION OF WHY THE FIRST EXAMPLE
// FROM THE OP IS WRONG
var alertResult = window.alert("inside aJax statement");
$.ajax({
    type: 'POST',
    url: "/getCodes.php?codes=billingCodes&parent=" + $('#wClient').val(),
    dataType: 'json',
    success: alertResult

})

E.g., first the alert is called and displayed, then the ajax call occurs with success referencing the return value from alert (which is probably undefined).

例如,首先调用并显示警报,然后在成功引用警报的返回值(可能未定义)时发生ajax调用。

Your second example is correct. If you're not seeing the alert in your second example, it means that the ajax call is not completing successfully. Add an error callback to see why.

你的第二个例子是正确的。如果您在第二个示例中没有看到警报,则表示ajax调用未成功完成。添加错误回调以查看原因。

#3


0  

In first case window.alert is executed immidiatly when you run $.ajax In second it run only when you receive answer from server, so I suspect that something wrong in you ajax request

在第一种情况下,当你运行$ .ajax时,window.alert会立即执行。第二种情况,它只在你收到服务器的答案时运行,所以我怀疑你的ajax请求有问题

#4


0  

You may want to try and use a promise:

您可能想尝试使用承诺:

var promise = $.ajax({
  type: 'POST',
  url: "/getCodes.php?codes=billingCodes&parent="+$('#wClient').val(),
  dataType: 'json'
});

promise.fail( function() {
  window.alert("Fail!");
});

promise.done( function() {
  window.alert("Success!");
});

What this does is saves the ajax call to a variable, and then assigns additional functionality for each of the return states. Make sure that the data type you are returning is actually json, though, or you may see strange behavior!

这样做是将ajax调用保存到变量,然后为每个返回状态分配其他功能。确保您要返回的数据类型实际上是json,或者您可能会看到奇怪的行为!

Note that js is single-threaded; the reason your first example works is because it actually executes the code next 'success' and stores the result. In this case there is nothing to store; it just pops an alert window. That means that the ajax call is leaving the client after the alert is fired: use the developer tools on Chrome or equivalent to see this.

注意js是单线程的;你的第一个例子工作的原因是因为它实际上执行下一个'成功'的代码并存储结果。在这种情况下,没有什么可以存储;它只是弹出一个警报窗口。这意味着在警报触发后,ajax调用将离开客户端:使用Chrome上的开发人员工具或等效工具来查看此信息。

By putting a function there, you assign it to do something when the ajax call returns much later in the thread (or, more precisely, in a new thread started when the response comes back).

通过在那里放置一个函数,当ajax调用在线程中稍后返回时(或者更准确地说,在响应返回时启动的新线程中),将其指定为执行某些操作。

#5


0  

I think that you do it right, but your request does not succeeds. Try add also error handler:

我认为你做得对,但你的要求没有成功。尝试添加错误处理程序:

error: function(){alert("Error");};

I guess that dataType does not match or something like that.

我猜数据类型不匹配或类似的东西。

#6


0  

It is 100% your second example is correct. Why it does nothing? Maybe because there is no success in the ajax call. Add "error" handler and check waht does your ajax call return with the browsers' developer tool -> Network -> XHR . This really helps in handling of broken / incorrect ajax requests

这是100%你的第二个例子是正确的。它什么都不做?也许是因为ajax调用没有成功。添加“错误”处理程序并检查waht使用浏览器的开发人员工具 - >网络 - > XHR返回您的ajax调用。这确实有助于处理损坏/不正确的ajax请求

#1


5  

In your second example nothing will happen unless you get a successful call back from the server. Add an error callback as many here have suggested to see that indeed the ajax request is working but the server is not currently sending a valid response.

在您的第二个示例中,除非您从服务器成功回拨,否则不会发生任何事情。添加一个错误回调,正如许多人建议的那样,确实ajax请求确实有效,但服务器当前没有发送有效的响应。

$.ajax({
        type: "POST",
        url: "/getCodes.php?codes=billingCodes&parent="+$('#wClient').val(),                        
        dataType:"json",
        success: function(response){
                alert(response);        
      },
      error: function(jqXHR, textStatus, errorThrown){
          alert('error');
      }         
      });

helpful Link in tracking down errors.

有用的链接跟踪错误。

#2


2  

Your first example does nothing whatsoever to prove that the ajax call has worked. All it does is prove that the ajax function was reached, because the values of the properties in the anonymous object you're passing into the ajax function are evaluated before the function is called.

你的第一个例子没有做任何事情来证明ajax调用有效。所有这一切都证明了ajax函数已经达到,因为在调用函数之前会评估您传递给ajax函数的匿名对象中的属性值。

Your first example is basically the same as this:

你的第一个例子与此基本相同:

// THIS IS NOT A CORRECTION, IT'S AN ILLUSTRATION OF WHY THE FIRST EXAMPLE
// FROM THE OP IS WRONG
var alertResult = window.alert("inside aJax statement");
$.ajax({
    type: 'POST',
    url: "/getCodes.php?codes=billingCodes&parent=" + $('#wClient').val(),
    dataType: 'json',
    success: alertResult

})

E.g., first the alert is called and displayed, then the ajax call occurs with success referencing the return value from alert (which is probably undefined).

例如,首先调用并显示警报,然后在成功引用警报的返回值(可能未定义)时发生ajax调用。

Your second example is correct. If you're not seeing the alert in your second example, it means that the ajax call is not completing successfully. Add an error callback to see why.

你的第二个例子是正确的。如果您在第二个示例中没有看到警报,则表示ajax调用未成功完成。添加错误回调以查看原因。

#3


0  

In first case window.alert is executed immidiatly when you run $.ajax In second it run only when you receive answer from server, so I suspect that something wrong in you ajax request

在第一种情况下,当你运行$ .ajax时,window.alert会立即执行。第二种情况,它只在你收到服务器的答案时运行,所以我怀疑你的ajax请求有问题

#4


0  

You may want to try and use a promise:

您可能想尝试使用承诺:

var promise = $.ajax({
  type: 'POST',
  url: "/getCodes.php?codes=billingCodes&parent="+$('#wClient').val(),
  dataType: 'json'
});

promise.fail( function() {
  window.alert("Fail!");
});

promise.done( function() {
  window.alert("Success!");
});

What this does is saves the ajax call to a variable, and then assigns additional functionality for each of the return states. Make sure that the data type you are returning is actually json, though, or you may see strange behavior!

这样做是将ajax调用保存到变量,然后为每个返回状态分配其他功能。确保您要返回的数据类型实际上是json,或者您可能会看到奇怪的行为!

Note that js is single-threaded; the reason your first example works is because it actually executes the code next 'success' and stores the result. In this case there is nothing to store; it just pops an alert window. That means that the ajax call is leaving the client after the alert is fired: use the developer tools on Chrome or equivalent to see this.

注意js是单线程的;你的第一个例子工作的原因是因为它实际上执行下一个'成功'的代码并存储结果。在这种情况下,没有什么可以存储;它只是弹出一个警报窗口。这意味着在警报触发后,ajax调用将离开客户端:使用Chrome上的开发人员工具或等效工具来查看此信息。

By putting a function there, you assign it to do something when the ajax call returns much later in the thread (or, more precisely, in a new thread started when the response comes back).

通过在那里放置一个函数,当ajax调用在线程中稍后返回时(或者更准确地说,在响应返回时启动的新线程中),将其指定为执行某些操作。

#5


0  

I think that you do it right, but your request does not succeeds. Try add also error handler:

我认为你做得对,但你的要求没有成功。尝试添加错误处理程序:

error: function(){alert("Error");};

I guess that dataType does not match or something like that.

我猜数据类型不匹配或类似的东西。

#6


0  

It is 100% your second example is correct. Why it does nothing? Maybe because there is no success in the ajax call. Add "error" handler and check waht does your ajax call return with the browsers' developer tool -> Network -> XHR . This really helps in handling of broken / incorrect ajax requests

这是100%你的第二个例子是正确的。它什么都不做?也许是因为ajax调用没有成功。添加“错误”处理程序并检查waht使用浏览器的开发人员工具 - >网络 - > XHR返回您的ajax调用。这确实有助于处理损坏/不正确的ajax请求