1

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?

maya91
  • 47
  • 9
  • 1
    the parameters are filled-in according to a pre-set order. jq passes 3 args in total. – dandavis Nov 29 '15 at 10:03
  • it's just basic javascript function calls – Jaromanda X Nov 29 '15 at 10:08
  • deferred is an object returned by `$.ajax`. You can also call it a promise. – jcubic Nov 29 '15 at 10:09
  • `jqXHR.done(function( data, textStatus, jqXHR ) {});` _An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details._ so yes – mplungjan Nov 29 '15 at 10:11
  • @jcubic Acctually, `deferred` is not the `promise`. You return `deferred.promise`. But yes, `$.ajax` returns a `promise`. – Arg0n Nov 29 '15 at 10:12

3 Answers3

3

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. three arguments passed are data, textStatus ,jqXHR

so your success function's first argument would be the response of the ajax call,i.e data recieved from the server, second argument would be the status and third argument would the the jqueryXmlHttpRequest object.

based on your need you can access these objects in the same order

Neel
  • 86
  • 3
  • I think I kind of get it now, so the success function we created is a callback function, and we can create any function to be a callback function for .done, it doesn't have to be a success function as long as I pass it the three argument and insert it into the .done function right? – maya91 Nov 29 '15 at 13:56
  • Yup any function can be added as callback function for .done() but the arguments are in the same order as mentioned – Neel Nov 30 '15 at 10:45
1
  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()

Community
  • 1
  • 1
ashishmohite
  • 1,120
  • 6
  • 14
0

The .done method accepts a "success" function as input.

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; 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.

So, the first argument (call it whatever - data / result / x / etc) will contain the reply of the server.

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

Lavi Avigdor
  • 4,092
  • 3
  • 25
  • 28
  • but the guy who wrote the code used the function reserved word so that means it's a function he defines himself rather than the function from the JQuery library.. – maya91 Nov 29 '15 at 13:35
  • I don't this I explained this thoroughly but I meant the done's argument being given a custm name not the success function's argument.. I tried writing the code like this $('document').ready(function(){ $.ajax({ url: "AwesomeText.html", type: "GET", datatype: "Html" }) .done(t) }); function t(result){ $("p").append(result); } and it still worked even though there is no success function – maya91 Nov 29 '15 at 13:37
  • If you don't provide the success callback it will still work as you have `.done(t)` which is sort of promise so it will be called after ajax request and response is done and will send the result as a parameter to `t` function – ashishmohite Nov 29 '15 at 13:52