Passing parameters to a function in Ajax

maya91

I'm following a tutorial on using Ajax with JavaScript, and this was the code we're making:

    $('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. I took out success and passed in x as an argument and it still worked..

so my question is how does JQuery know it should store the requested HTML document in this parameter we're creating in this function? what if I have other functions will Jquery store the requested file to every function in it's parameter? I don't get how JQuery knows.

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?

ashishmohite
  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

Documentation

success = Type: Function( Anything data, String 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

You can check following question for understanding the promises and .done() method

jQuery deferreds and promises - .then() vs .done()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing parameters to a function in Ajax

From Dev

passing multiple parameters using $.ajax by POST to php function

From Dev

Passing parameters to function closure

From Java

Passing parameters to a Bash function

From Dev

Passing tuples to function parameters

From Dev

Passing "*&" parameters to a function

From Dev

Passing parameters to a function in yii

From Dev

Passing parameters to a resolve function

From Dev

Passing SQL function parameters

From Dev

passing parameters in python function

From Dev

Passing dynamic parameters into a function

From Dev

Passing parameters to a resolve function

From Dev

Passing parameters to a Matlab function

From Dev

Passing parameters with space to function

From Dev

Passing parameters to a BASH function

From Dev

Passing function references as parameters

From Dev

passing parameters to function

From Dev

passing additional parameters to two function

From Dev

Passing extra parameters into the stat function

From Dev

Passing selectors as function parameters in jQuery

From Dev

Function in onclick of tag <a> passing parameters

From Dev

Passing function parameters and no return statement

From Dev

passing two parameters to fminunc function

From Dev

Passing on the parameters of a function into another one?

From Dev

Calling or Passing Optional Parameters to Function

From Dev

Passing a list of parameters into a Python function

From Dev

Passing SOME of the parameters to a function in python

From Dev

Passing a function with parameters to mapped elements

From Dev

Function in onclick of tag <a> passing parameters