jQuery Jquery检查ajax帖子是否成功

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

Jquery checking success of ajax post

jquery

提问by zsharp

how do i define the success and failure function of an ajax $.post?

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

回答by Jere.Jones

The documentation is here: http://docs.jquery.com/Ajax/jQuery.ajax

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

But, to summarize, the ajax call takes a bunch of options. the ones you are looking for are error and success.

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

You would call it like this:

你会这样称呼它:

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

I have shown the success and error function taking no arguments, but they can receive arguments.

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

The error function can take three arguments: XMLHttpRequest, textStatus, and errorThrown.

error 函数可以接受三个参数:XMLHttpRequest、textStatus 和 errorThrown。

The success function can take two arguments: data and textStatus. The page you requested will be in the data argument.

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

回答by Matthew Crumley

If you need a failure function, you can't use the $.get or $.post functions; you will need to call the $.ajax function directly. You pass an options object that can have "success" and "error" callbacks.

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

Instead of this:

取而代之的是:

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

you would use this:

你会用这个:

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

There are lots of other options available too. The documentationlists all the options available.

还有很多其他选项可用。该文档列出了所有可用的选项。

回答by Li3ro

using jQuery 1.8 and above, should use the following:

使用 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"); });

check out the docs as @hitautodestruct stated.

查看@hitautodestruct 所述的文档。

回答by yashpal

I was wondering, why they didnt provide in jquery itself, so i made a few changes in jquery file ,,, here are the changed code block:

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

original Code block:

原始代码块:

    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
    });  

Changed Code block:

更改代码块:

        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
        });
    },

This should help the one trying to catch error on $.Post in jquery.

这应该有助于试图在 jquery 中捕获 $.Post 上的错误的人。

Updated: Or there is another way to do this is :

更新:或者还有另一种方法来做到这一点:

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

回答by Veenkar

This style is also possible:

这种风格也是可能的:

$.get("mypage.html")
    .done(function(result){
        alert("done. read "+result.length+" characters.");
    })
    .fail(function(jqXHR, textStatus, errorThrown){
        alert("fail. status: "+textStatus);
    })