jQuery检查Ajax发布成功


67

我如何定义ajax $ .post的成功和失败功能?

Answers:


116

文档在这里:http : //docs.jquery.com/Ajax/jQuery.ajax

但是,总而言之,ajax调用需要很多选择。您要寻找的是错误和成功。

您可以这样称呼它:

$.ajax({
  url: 'mypage.html',
  success: function(){
    alert('success');
  },
  error: function(){
    alert('failure');
  }
});

我已经展示了成功和错误函数不带参数,但是它们可以接收参数。

错误函数可以使用三个参数:XMLHttpRequest,textStatus和errorThrown。

成功函数可以使用两个参数:data和textStatus。您请求的页面将在data参数中。


28
请在此处注明弃用通知:bit.ly/nuiyyE。从jQuery 1.8开始,将不赞成使用成功,错误和完整方法,而应使用:完成,失败和始终执行。
hitautodestruct

15
如果我没看错,则弃用通知适用于返回的jqXHR对象的success()等,而不适用于使用settings对象指定的通知。因此$.ajax(url).success(...)将不建议使用,$.ajax(url).done(...)而应改用。$.ajax(url, {success: ...}还是可以的,我看不到这些设置成员的个别说明上的弃用通知。
亚历山大·克里姆采切克

1
这些答案之一记录了@hitautodestruct提到的内容。如果愿意,请做出可接受的答案。
MrBoJangles

我以与@AlexanderKlimetschek相同的方式解释文档和弃用声明。但是,即使未弃用,文档也指出error callback handler is *not* called for cross-domain script and cross-domain JSONP requests。这是切换到将fail函数链接到jqXHR对象的模式的动机,而不是使用error选项。
subsci 2014年

46

如果需要失败函数,则不能使用$ .get或$ .post函数。您将需要直接调用$ .ajax函数。您传递的选项对象可以具有“成功”和“错误”回调。

代替这个:

$.post("/post/url.php", parameters, successFunction);

您将使用此:

$.ajax({
    url: "/post/url.php",
    type: "POST",
    data: parameters,
    success: successFunction,
    error: errorFunction
});

也有许多其他选择。该文档列出了所有可用的选项。


5
在url.php调用过程中,是否在服务器端检查了任何特定内容?就像我如何知道通话成功还是错误?我是否必须返回true / false,null或某种形式的东西?我正在struts上尝试此操作,并且有点困惑我的动作课应该返回什么。正如如何将我知道的东西经历了成功或没有..
考希克戈帕尔

9
@Kaushik:它基于服务器返回的HTTP状态代码。基本上,除2xx或304(未修改)以外的任何内容都被视为错误。其他条件是连接超时或由于某种原因而无法连接到服务器。
马修·克鲁姆利

33

使用jQuery 1.8及更高版本,应使用以下内容:

var request = $.ajax({
    type: 'POST',
    url: 'mmm.php',
    data: { abc: "abcdefghijklmnopqrstuvwxyz" } })
    .done(function(data) { alert("success"+data.slice(0, 100)); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

按照@hitautodestruct所述查看文档。


这可以用于$ .post和$ .get!如文档中
Brainfeeder 2013年

2

我想知道为什么他们没有在jquery本身中提供,所以我在jquery文件中做了一些更改,以下是更改后的代码块:

原始代码块:

    post: function( url, data, callback, type ) {
    // shift arguments if data argument was omited
    if ( jQuery.isFunction( data ) ) {
        type = type || callback;
        callback = data;
        data = {};
    }

    return jQuery.ajax({
        type: "POST",
        url: url,
        data: data,
        success: callback,
        dataType: type
    });  

更改的代码块:

        post: function (url, data, callback, failcallback, type) {
        if (type === undefined || type === null) {
            if (!jQuery.isFunction(failcallback)) { 
            type=failcallback
        }
        else if (!jQuery.isFunction(callback)) {
            type = callback
        }
        }
        if (jQuery.isFunction(data) && jQuery.isFunction(callback)) {
            failcallback = callback;

        }
        // shift arguments if data argument was omited
        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = {};

        }


        return jQuery.ajax({
            type: "POST",
            url: url,
            data: data,
            success: callback,
            error:failcallback,
            dataType: type
        });
    },

这应该可以帮助试图在jquery中捕获$ .Post错误的人。

更新:或者还有另一种方法可以做到:

   $.post(url,{},function(res){
        //To do write if call is successful
   }).error(function(p1,p2,p3){
             //To do Write if call is failed
          });

你叫什么post:符号?一样$.post()吗?
ahnbizcad 2014年

0

这种样式也是可能的:

$.get("mypage.html")
    .done(function(result){
        alert("done. read "+result.length+" characters.");
    })
    .fail(function(jqXHR, textStatus, errorThrown){
        alert("fail. status: "+textStatus);
    })
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.