jquery-ajax
Jquery

Difference between jQuery ajax(), get() and post().

Spread the love

jQuery get() and post() contain features that are subsets of jQuery.ajax().

Although it’s an honest idea to know Ajax within the context of native JavaScript, leveraging the facility of JavaScript libraries for your Ajax calls isn’t a nasty idea. counting on the dimensions and complexity of your application, it can often minimize the quantity of code you would like to write down so as to supply the simplest cross-browser experience.

You’ll see that the most important difference from an implementation standpoint is that you simply pass an object to jQuery.ajax(), which contains the required parameters. And as for jQuery.get() and jQuery.post(), here you pass in arguments. We have discussed those three jquery methods

#Example 1

jQuery.ajax({

url: ‘test.txt’,

dataType: ‘text’,

type: “GET”,  

success: function(data) {    console.log(data);                }

});

So, in Example # 1, we use the above mentioned method. There are certainly more configurable parameters, but here we are utilizing just required minimum.

We just pass an object into the above ajax method. That object has four properties: “url,” “dataType,” “type,”  and “success”, and here are the details for each property:

  • url: This is the URL of the file that you want to send data via your ajax call .
  • dataType: This determines how the return data will be treated (i.e. pure text, html, XML, etc.).
  • type: This the the request type. Use “GET” or “POST”. This is actually optional; if you omit it, jQuery will default to “GET”.
  • success: This is a callback function that is fired after a successful http request has completed. The first argument to the function is the data returned from the server. There are other arguments that can be passed in as well.

# Example 2

jQuery.get(‘test.txt’,function(data){    

console.log(data)

},’text’);

When using above, instead of passing in an object, you pass in arguments. At minimum, you’ll need the first two arguments: the URL of the file you want to retrieve (i.e. ‘test.txt’), and a success callback. In this example, we also passed in a third argument: ‘text,’ which told jQuery that we wanted to treat the return message as text.

The above method is recommended when you want to make a dirty Ajax call.

# Example 3

JQuery.post(‘test.txt’,function(data){   

 console.log(‘post response-> ‘ + data)},

‘text’);

And in exactly the same manner as get(), this method is like using ajax(), and specifying a “type” of “POST.” So, this method is recommended if you need to make a quick Ajax call via POST .

If you’re wondering whether you ought to use one of three above methods , it’s really up to you. a method to approach it’d be to ask yourself: “Am I writing a multi-purpose function which will be ready to perform either a GET or a POST? and that I do i would like this function to accommodate dynamic settings like dataType or data?” If the solution to those questions is “yes,” then use jQuery.ajax() .

Leave a Reply

Your email address will not be published.