检查 Jquery ajax 加载成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/936357/
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 10:11:06  来源:igfitidea点击:

Checking Jquery ajax load success

jqueryajax

提问by Mark

I've been using thisto load another page, and just using their example :

我一直在用来加载另一个页面,只是使用他们的例子:

$("#links").load("/Main_Page #jq-p-Getting-Started li");

However I want to run a function when the load is successful, and a different one if it fails, unfortunately the description on the callback says:

但是我想在加载成功时运行一个函数,如果失败则运行一个不同的函数,不幸的是回调的描述说:

The function called when the ajax request is complete (not necessarily success).

ajax请求完成时调用的函数(不一定成功)。

How do I make sure the load was successful?

如何确保加载成功?

Thanks.

谢谢。

采纳答案by Ender

You might try using the more detailed $.ajax() method call. This will give you more fine-grained control over the AJAX call (but it also requires a bit more work than a simple .load().) It will allow you to call different functions based on whether your call returns success or error.

您可以尝试使用更详细的 $.ajax() 方法调用。这将使您对 AJAX 调用进行更细粒度的控制(但它也需要比简单的 .load() 做更多的工作。)它将允许您根据调用返回成功还是错误来调用不同的函数。

Check out: http://docs.jquery.com/Ajax/jQuery.ajax#optionsfor documentation on this function. If you click the "Options" tab on that page, you will get a full listing of the parameters that you can pass to the $.ajax() function.

查看:http: //docs.jquery.com/Ajax/jQuery.ajax#options有关此功能的文档。如果单击该页面上的“选项”选项卡,您将获得可以传递给 $.ajax() 函数的参数的完整列表。

回答by cgp

Check the textStatusof the response.

检查响应的textStatus

$("#links").load("/Main_Page #jq-p-Getting-Started li", 
  function (responseText, textStatus, XMLHttpRequest) {
    if (textStatus == "success") {
         // all good!
    }
    if (textStatus == "error") {
         // oh noes!
    }
  }

In determination to figure out the issue:

为了解决这个问题:

A JSBin example of successand fail

的JSBin例子successfail

回答by chaos

The second argument to your callback is the string status value for the request, which will be one of "beforeSend", "success", "complete", and "error".

第二个参数到您的回调是请求字符串状态值,这将是一个"beforeSend""success""complete",和"error"

回答by Rodrigo Prazim

I use this function and it worked for me:

我使用这个功能,它对我有用:

$.ajax({
   url: "demo_test.txt", 
   success: function(result){
       location:reload();
   }
});