解释jQuery AJAX成功方法

Exi*_*tos 8 javascript ajax jquery

我试图使用这个jQuery脚本,这让我很困惑:

function CallService() 
        {
                $.ajax({
                    type        : varType, //GET or POST or PUT or DELETE verb
                    url         : varUrl, // Location of the service
                    data        : varData, //Data sent to server
                    contentType : varContentType, // content type sent to server
                    dataType    : varDataType, //Expected data format from server
                    processdata : varProcessData, //True or False
                    success     : function(msg) {//On Successfull service call
                    ServiceSucceeded(msg);                    
                    },
                    error: ServiceFailed// When Service call fails
                });
        }
Run Code Online (Sandbox Code Playgroud)

我很困惑的是成功对象.jQuery文档说:

success(data, textStatus, jqXHR)Function, Array

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; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
Run Code Online (Sandbox Code Playgroud)

但是这种方法签名看起来不像:

success     : function(msg) {//On Successfull service call
                        ServiceSucceeded(msg);                    
                        }
Run Code Online (Sandbox Code Playgroud)

我们似乎正在传递的对象.

问题:

1)什么function(msg){ServiceSucceeded(msg)}意思?

2)在这种情况下什么是'msg'?

3)我到底想知道如何构建成功方法sugnature?

T.J*_*der 15

完全合理的问题.:-)在JavaScript中,您不一定要调用具有尽可能多的args的函数,并且您不必定义尽可能多的args.如果您习惯于更受限制的环境,这可能会令人困惑.:-)

回答具体问题:

1)function(msg){ServiceSucceeded(msg)}是什么意思?

它定义了一个函数(一个匿名函数),它接受一个命名参数(msg)并调用ServiceSucceded传入该arg.jQuery将使用函数的jQuery文档定义的三个参数调用该函数success,但是这个特定的success函数只使用那些(data)中的第一个.更多关于命名函数与匿名函数的信息.

2)在这种情况下什么是'msg'?

函数的第一个参数.jQuery的文档称之为第一个参数data,但你可以随意调用它.

3)我到底想知道如何构建成功方法sugnature?

你做了正确的事,它在jQuery文档中.

关于函数参数的这个问题可能令人困惑,所以让我们举一些例子:

function foo(arg) {
    alert(arg);
}
Run Code Online (Sandbox Code Playgroud)

这是非常清楚的,我正在定义一个名为的函数foo,它接受一个命名参数arg.因此:

foo("Hi there"); // alerts "Hi there"
Run Code Online (Sandbox Code Playgroud)

但我也可以这样做:

foo(); // alerts "undefined"
Run Code Online (Sandbox Code Playgroud)

在那里,我没有给任何参数foo,所以内foo,arg是不确定的.

我也可以这样做:

foo("Hi there", "again"); // alerts "Hi there"
Run Code Online (Sandbox Code Playgroud)

foo两个参数调用,但foo只使用其中一个.

我可以定义foo使用尽可能多的参数:

function foo() {
    var index;

    for (index = 0; index < arguments.length; ++index) {
        alert(arguments[index]);
    }
}
Run Code Online (Sandbox Code Playgroud)

arguments是一个所有函数都具有的自动函数,它是Array函数被调用的实际参数的伪数组(它不是真正的数组).所以:

foo("Hi there", "again"); // alerts "Hi there", and then alerts "again"
Run Code Online (Sandbox Code Playgroud)

您甚至可以混合命名和未命名的参数:

function foo(arg) {
    var index;

    alert(arg);
    for (index = 1; index < arguments.length; ++index) {
        alert("[" + arguments[index] + "]");
    }
}
Run Code Online (Sandbox Code Playgroud)

所以现在

foo("Hi there", "again"); // alerts "Hi there" and then alerts "[again]"
Run Code Online (Sandbox Code Playgroud)

请注意[]第二个警报,因为我开始使用索引1而不是零循环.

arguments 和命名args连接:

function foo(arg) {
    alert("arg = " + arg);
    alert("arguments[0] = " + arguments[0]);
    arg = "Updated";
    alert("arg = " + arg);
    alert("arguments[0] = " + arguments[0]);
}
Run Code Online (Sandbox Code Playgroud)

如果我这样做foo("Hi");,则显示以下警报:

arg = Hi
arguments[0] = Hi
arg = Updated
arguments[0] = Updated

(如果你更新,它也是另一种方式arguments[0].)

  • 嗨,TJ,你的回答真的很有帮助,非常感谢我能看到现在发生的事情只会简单一些. (2认同)

Roc*_*mat 6

所述函数传递3个参数:data,statusjqXHR对象. data是从AJAX调用返回的,status是HTTP状态代码(我认为),jqXHR是一个jQuery包装的XHR对象.

在这个脚本中,他们只关心数据参数,而不关心其他两个.

所以使用success: function(msg),他们只获得data参数.其他两个被发送,但被忽略.

ServiceSucceeded只是一个函数,使用data发送给它的参数调用.

success: ServiceSucceeded 也可以在这里工作.