简体   繁体   English

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

[英]$.ajax() success won't run function

My question regards the $.ajax() jQuery method. 我的问题是关于$.ajax() jQuery方法。 I can't get the success parameter in $.ajax() to work. 我无法在$.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. 在第一种情况下,我得到一个JavaScript警报窗口,让我知道我调用的$.ajax()正在工作。 All that is changed in the second block of code is I put the window.alert() inside a function() { window.alert(); } 在第二个代码块中更改的是我将window.alert()放在function() { window.alert(); } function() { 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(){}放入一些实际有用的代码。

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. 有用的链接跟踪错误。

Your first example does nothing whatsoever to prove that the ajax call has worked. 你的第一个例子没有做任何事情来证明ajax调用有效。 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函数的匿名对象中的属性值。

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

})

Eg, first the alert is called and displayed, then the ajax call occurs with success referencing the return value from alert (which is probably undefined ). 例如, 首先调用并显示alert然后发生ajax调用, success引用alert的返回值(可能是undefined )。

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. 如果您在第二个示例中没有看到警报,则表示ajax调用未成功完成。 Add an error callback to see why. 添加error回调以查看原因。

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. 这样做是将ajax调用保存到变量,然后为每个返回状态分配其他功能。 Make sure that the data type you are returning is actually json, though, or you may see strange behavior! 确保您要返回的数据类型实际上是json,或者您可能会看到奇怪的行为!

Note that js is single-threaded; 请注意,js是单线程的; 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. 这意味着在警报触发后,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调用在线程中稍后返回时(或者更准确地说,在响应返回时启动的新线程中),将其指定为执行某些操作。

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

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. 我猜数据类型不匹配或类似的东西。

It is 100% your second example is correct. 这是100%你的第二个例子是正确的。 Why it does nothing? 它什么都不做? Maybe because there is no success in the ajax call. 也许是因为ajax调用没有成功。 Add "error" handler and check waht does your ajax call return with the browsers' developer tool -> Network -> XHR . 添加“错误”处理程序并检查waht使用浏览器的开发人员工具 - >网络 - > XHR返回您的ajax调用。 This really helps in handling of broken / incorrect ajax requests 这确实有助于处理损坏/不正确的ajax请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM