Does anyone know what is the difference between $("#id").load and $.ajax?
Does anyone know what is the difference between $(#id).load and $.ajax ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let me clarify things for you a little bit :
$.ajax()is the basic and low-level ajax function jQuery provides which means you can do what ever you want to like you can work withXmlHttpRequestobject. But once upon a time jQuery Developers thought that actually besides$.ajax(), they could provide more specific methods to developers so they wouldn’t need to pass more parameters to make$.ajax()method work the way they want. For example they said instead of passingjsonas a parameter to$.ajax()to indicate return data type, they provided$.getJSON()so we would all know that the return type we expected wasjson, or instead of indicating send method aspostorget, you could use$.post()or$.get()respectively.So
load()is the same thing, it can help you inject html data into your html. withload()method you know that an html portion is being expected.Isn’t that cool ?
I think I’ve been fallen in love.
For more information, you can visit jquery.com, they are even providing their new library and api tutorial page.
Edit :
is the same as below :
Now as you can see it is the simplified version of
$.ajax(), to make post call, you need to pass some information of send method type which ispostas shown at the first example but instead of doing this you can use$.post()because you know what you are doing ispostso this version is more simplified and easy to work on.But don’t forget something. Except for
load(), all other ajax methods return XHR (XmlHttpRequest instance) so you can treat them as if you were working with XmlHttpRequest, actually you are working with it tho 🙂 and butload()returns jQuery which means :in the example above, you can easly inject the return html into
#objectIDelement. Isn’t it cool ? If it wasn’t returning jQuery, you should have been working with callback function where you probably get the result out of likedataobject and inject it manually into the html element you want. So it would be hassle but with$.load()method, it is really simplified in jQuery.You can even post parameters, so according to those parameters you can do some work at server-side and send html portion back to the client and your cute jQuery code takes it and inject it into
#feedshtml element in the example right above.