简体   繁体   English

将参数传递给Ajax中的函数

[英]Passing parameters to a function in Ajax

I'm following a tutorial on using Ajax with JavaScript, and this was the code we're making: 我正在跟踪有关将Ajax与JavaScript结合使用的教程,这是我们正在编写的代码:

    $('document').ready(function(){
        $.ajax({
            url: "AwesomeText.html",
            type: "GET",
            datatype: "Html"    
    })
    .done(Success)      
    });
    function Success(result){
        $("p").append(result);     
    }   

I got how it worked but there is one thing that confused me. 我知道它是如何工作的,但有一件事使我感到困惑。 We made a Success function to append the requested HTML file to the paragraph in the page, and then we passed a parameter called result, so from what I understand I can name the parameter anything and pass it as an argument and it will work. 我们做了一个成功函数,将请求的HTML文件附加到页面的段落中,然后传递了一个名为result的参数,因此据我所知,我可以给该参数起任何名字,并将其作为参数传递,它将起作用。 I took out success and passed in x as an argument and it still worked.. 我取得了成功,并将x作为参数传递,但仍然有效。

so my question is how does JQuery know it should store the requested HTML document in this parameter we're creating in this function? 所以我的问题是JQuery如何知道它应该在我们在此函数中创建的此参数中存储请求的HTML文档? what if I have other functions will Jquery store the requested file to every function in it's parameter? 如果我还有其他功能,Jquery将请求的文件存储到参数中的每个函数中怎么办? I don't get how JQuery knows. 我不知道JQuery怎么知道。

Also, there is a .done function in there, is that the same function that's explained here: https://api.jquery.com/deferred.done/ why is it called deferred? 另外,那里还有一个.done函数,这里解释的是同一函数: https : //api.jquery.com/deferred.done/为什么将其称为deferred?

Default .done() callbacks will have 3 arguments passed through so based on your need you can use any of them in your success function with your own name. 默认的.done()回调将传递3个参数,因此根据您的需要,您可以在自己的成功函数中使用任何名称。 three arguments passed are data, textStatus ,jqXHR 传递的三个参数是data,textStatus,jqXHR

so your success function's first argument would be the response of the ajax call,ie data recieved from the server, second argument would be the status and third argument would the the jqueryXmlHttpRequest object. 因此,成功函数的第一个参数将是ajax调用的响应,即从服务器接收到的数据,第二个参数将是status,第三个参数将是jqueryXmlHttpRequest对象。

based on your need you can access these objects in the same order 根据您的需要,您可以按相同的顺序访问这些对象

  1. When you pass url to ajax it basically fetches that url and returns whatever it got at that url , AwesomeText.html in your case and then it sends the content of that url to success function as a first parameter 当你通过url来阿贾克斯它基本上取入该URL并返回任何在这一点得到了urlAwesomeText.html在你的情况,然后发送该URL到内容success函数作为第一个参数

Documentation 文献资料

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

  1. deferred .done(fn) method makes calling some method after the method on which .done was called [You can say Promise ] which makes synchronous calls deferred .done(fn)方法在调用.done(fn)方法之后调用某个方法[您可以说Promise ]进行同步调用

You can check following question for understanding the promises and .done() method 您可以检查以下问题以了解Promise和.done()方法

jQuery deferreds and promises - .then() vs .done() jQuery的延迟和承诺-.then()与.done()

The .done method accepts a "success" function as input. .done方法接受“成功”功能作为输入。

Success: Function( Anything data, String 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. 每个函数将依次调用。

So, the first argument (call it whatever - data / result / x / etc) will contain the reply of the server. 因此,第一个参数(随便叫它-数据/结果/ x /等)将包含服务器的答复。

Source: http://api.jquery.com/jquery.ajax/ 资料来源: http//api.jquery.com/jquery.ajax/

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

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