解释jQuery AJAX成功方法

时间:2011-05-18 15:16:53

标签: javascript jquery ajax

我试图使用这个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
                });
        }

我很困惑的是成功对象。 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.

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

success     : function(msg) {//On Successfull service call
                        ServiceSucceeded(msg);                    
                        }

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

问题:

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

2)在这种背景下什么是'msg'?

3)我究竟想知道如何构建sugnature for sucess的方法?

7 个答案:

答案 0 :(得分:15)

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

回答具体问题:

  

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

它定义了一个函数(一个匿名函数),它接受一个命名参数(msg)并调用传递该arg的ServiceSucceded。 jQuery将使用success函数的jQuery文档定义的三个参数调用该函数,但是这个特定的success函数仅使用其中的第一个(data)。有关命名函数与匿名函数here的更多信息。

  

2)在这种背景下什么是'msg'?

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

  

3)我究竟想知道如何构建sugnature for sucess的方法?

你做对了,它在jQuery文档中。

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

function foo(arg) {
    alert(arg);
}

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

foo("Hi there"); // alerts "Hi there"

但我也可以这样做:

foo(); // alerts "undefined"

在那里,我没有为foo提供任何论据,因此在foo中,arg未定义。

我也可以这样做:

foo("Hi there", "again"); // alerts "Hi there"

我使用两个参数调用foo,但foo仅使用其中一个参数。

我可以定义foo以使用与传入的一样多的参数:

function foo() {
    var index;

    for (index = 0; index < arguments.length; ++index) {
        alert(arguments[index]);
    }
}

arguments是所有函数都具有的自动函数,它是函数调用的实际参数的伪数组(它实际上不是Array)。所以:

foo("Hi there", "again"); // alerts "Hi there", and then alerts "again"

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

function foo(arg) {
    var index;

    alert(arg);
    for (index = 1; index < arguments.length; ++index) {
        alert("[" + arguments[index] + "]");
    }
}

所以现在

foo("Hi there", "again"); // alerts "Hi there" and then alerts "[again]"

请注意第二个警报周围的[],因为我开始使用索引1进行循环而不是零。

arguments和已命名的args已连接:

function foo(arg) {
    alert("arg = " + arg);
    alert("arguments[0] = " + arguments[0]);
    arg = "Updated";
    alert("arg = " + arg);
    alert("arguments[0] = " + arguments[0]);
}

如果我foo("Hi");,则会显示以下警告:

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

(如果您更新arguments[0],情况则相反。)

答案 1 :(得分:6)

该函数传递了3个参数:datastatusjqXHR对象。 data是从AJAX调用返回的内容,status是HTTP状态代码(我认为),jqXHR是jQuery包装的XHR对象。

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

因此,使用success: function(msg)时,他们只会获得data参数。其他两个被发送,但被忽略。

ServiceSucceeded只是一个通过发送data参数调用的函数。

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

答案 2 :(得分:2)

  1. 这意味着成功处理程序使用请求的响应调用ServiceSucceeded
  2. msg包含请求的响应。 msg映射到jQuery文档中的data
  3. 您需要查看jQuery文档以查找签名。

答案 3 :(得分:1)

1)如果AJAX请求成功,则调用该函数,即被联系的服务器返回成功状态代码。

2)我认为'msg'是从服务器返回的数据。其他两个参数未提供,因此未使用。

3)使用Jquery文档,然后摆弄,直到得到你想要的东西。

答案 4 :(得分:1)

  1. 这是匿名函数 它就像一个常规功能,但没有名字。

  2. msg是函数的第一个参数。

  3. 阅读文档。

答案 5 :(得分:1)

即使成功函数被定义为采用三个参数(根据你引用的文档),这三个参数不是强制性的 - Javascript对这类事情非常宽容;如果你错过了函数调用中的参数,它只会被设置为underfined,所以只要你不尝试使用它,JS就不会抛出任何错误。

您提供的代码只提供了一个参数 - msg - 但在JS中,这是完全有效的;它只是意味着msg将是文档中定义的data参数,textStatusjqXHR将是未定义的。

这很好,只要在你的成功函数中你实际上并不想使用这些参数中的任何一个。如果你想使用它们,那么传递它们,但如果没有,可以放弃它们。您正在编写成功函数,因此您可以决定使用哪三个参数。

答案 6 :(得分:1)

jquery Ajax是一种与服务器(PHP,ASP等)通信的方式。我们假设您使用PHP。函数“callService()”向“varUrl”(validation.php,ie)发送请求并获取(或POST - > varType)内容(varContentType - &gt; valdation.php?id = 1231&amp; whatever = soemthing) 。这样做的目的是获取一些服务器端数据而无需重新加载页面。如果你想验证validation.php echo 一些 html ,那么Ajax函数中的dataType必须是“html”。有关dataType的更多信息,请参阅jquery.com。

success参数是服务器响应的函数处理程序。如果从服务器获得与您提出的dataType相对应的响应(html,json,text等),则调用Success。在该特定情况下,如果服务器正确响应,则使用属性“msg”调用函数“ServiceSucceeded”,该属性是您要求的服务器响应。