简体   繁体   English

带有完整,成功和错误部分的标准AJAX调用应该如何使用

[英]How should be a standard AJAX call with complete, success and error section

I use the following AJAX call in order to make CRUD operations in MVC . 我使用以下AJAX调用来在MVC进行CRUD操作。 However, as there are many different usage of complete , success , error functions of AJAX calls, I want to be clarified want is the most common or suitable usage of them. 但是,由于AJAX调用的completesuccesserror函数有许多不同的用法,因此我想澄清一下,它们是最常见或最合适的用法。 In some examples, there is no parameter for these functions but in other examples there are several parameters (ie jqXHR , textStatus , errorThrown ). 在某些示例中,这些函数没有参数,但是在其他示例中,有几个参数(即jqXHRtextStatuserrorThrown )。 Of course I use the necessary operation for each case ie displaying notification or confirmation messages, etc. but I just need to the default or standart usage each of them. 当然,对于每种情况,我都会使用必要的操作,即显示通知或确认消息等,但是我只需要默认或标准用法即可。 Could you please a brief explanations and post the most suitable way for standard usage of AJAX for CRUD operations? 您能否简要说明一下,并发布最适合CRUD操作的AJAX标准用法的方法?

$.ajax({
    type: "POST",
    url: '@Url.Action("Insert", "Account")',
    cache: false,
    dataType: "json",
    data: formdata,
    complete: function () {
        //???
    },
    success: function (data, textStatus, XMLHttpRequest) {
        // ???
    },
    error: function (response) {
        // ???
    },

    //other example usages of error function :::
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
    },
    error: function (xhr) {
        console.log('Error: ' + xhr.statusText);
    }
}); 

You'll see different examples of these because: 您将看到这些示例的不同示例,因为:

  • Some of the arguments are optional or mixed type 一些参数是可选的或混合类型
  • Some of it, just depends on what the author decided to name the arguments in the callback functions. 其中一些,仅取决于作者决定在回调函数中命名参数的方式。
  • Some use of these callbacks and their arguments depend on the jQuery version the author was using at the time. 这些回调及其参数的某些用法取决于作者当时使用的jQuery版本。

I would recommend to always base your use of .ajax in accordance to documentation for the version that you are using. 我建议始终根据所使用版本的文档来确定对.ajax的使用。 So if you're looking for a standard way to use .ajax for your application, your best bet is to follow the documentation syntax: 因此,如果您正在寻找在应用程序中使用.ajax的标准方法,那么最好的选择就是遵循文档语法:

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

The documentation is very good at describing what changes came into play for what versions of jQuery, so by paying attention to that you can avoid problems in your application. 该文档非常擅长描述针对哪些版本的jQuery进行了哪些更改,因此通过注意,您可以避免应用程序中的问题。

From the jQuery documentation: 从jQuery文档中:

complete 完成

Type: Function( jqXHR jqXHR, String textStatus ) 类型:函数(jqXHR jqXHR,String textStatus)

A function to be called when the request finishes (after success and error callbacks are executed). 请求完成时(执行成功和错误回调之后)将调用的函数。 The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror"). 该函数传递了两个参数:jqXHR(在jQuery 1.4.x中,XMLHTTPRequest)对象和一个对请求状态进行分类的字符串(“成功”,“未修改”,“无内容”,“错误”,“超时”,“中止”或“ parsererror”)。 As of jQuery 1.5, the complete setting can accept an array of functions. 从jQuery 1.5开始,完整设置可以接受函数数组。 Each function will be called in turn. 每个函数将依次调用。 This is an Ajax Event. 这是一个Ajax事件。

success 成功

Type: Function( Anything data, String textStatus, jqXHR jqXHR ) 类型:函数(任何数据,字符串textStatus,jqXHR jqXHR)

A function to be called if the request succeeds. 请求成功时要调用的函数。 The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; 该函数将传递三个参数:服务器返回的数据,根据dataType参数或dataFilter回调函数(如果已指定)进行格式化; a string describing the status; 描述状态的字符串; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. 和jqXHR对象(在jQuery 1.4.x中为XMLHttpRequest)。 As of jQuery 1.5, the success setting can accept an array of functions. 从jQuery 1.5开始,成功设置可以接受一系列函数。 Each function will be called in turn. 每个函数将依次调用。 This is an Ajax Event. 这是一个Ajax事件。

error 错误

Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) 类型:函数(jqXHR jqXHR,字符串textStatus,字符串errorThrown)

A function to be called if the request fails. 如果请求失败,将调用的函数。 The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. 该函数接收三个参数:jqXHR对象(在jQuery 1.4.x中为XMLHttpRequest),一个描述错误类型的字符串,以及一个可选的异常对象(如果发生)。 Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". 第二个参数(除null外)的可能值为“超时”,“错误”,“中止”和“ parsererror”。 When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." 发生HTTP错误时,errorThrown会接收HTTP状态的文本部分,例如“未找到”或“内部服务器错误”。 As of jQuery 1.5, the error setting can accept an array of functions. 从jQuery 1.5开始,错误设置可以接受函数数组。 Each function will be called in turn. 每个函数将依次调用。 Note: This handler is not called for cross-domain script and cross-domain JSONP requests. 注意:对于跨域脚本和跨域JSONP请求,不会调用此处理程序。 This is an Ajax Event. 这是一个Ajax事件。

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

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