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?
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 parametersuccess = Type: Function( Anything data, String textStatus, jqXHR jqXHR )
.done(fn)
method makes calling some method after the method on which .done was called [You can say Promise
] which makes synchronous callsYou can check following question for understanding the promises and .done() method
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments