Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $.post and $.ajax?

Curious if anyone knows what the difference is in regards to the data parameter.

I have a $.post method that takes a $('#myform').serialize() as my data param and works.

If I try the same using the $.ajax() approach, it doesn't work as my data param doesn't appear correct.

Does anyone know the difference and what I might use instead of the above .serialize?

like image 497
Ed DeGagne Avatar asked Oct 10 '12 13:10

Ed DeGagne


People also ask

What is the difference between Ajax and POST?

$. post is a shorthand way of using $. ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code.

Should I use Ajax or POST?

Use POST if your call is going to write any data at all to the server. In fact, you should not only use this criterion for selecting between GET and POST for your Ajax calls but also for when selecting which should be used for processing forms on your web page.

Is Ajax a post request?

data : A plain object or string that is sent to the server with the request. success : A callback function that is executed if the request succeeds.it takes as an argument the returned data. It is also passed the text status of the response.

What is difference between GET and Ajax?

get() executes an Ajax GET request. The returned data (which can be any data) will be passed to your callback handler. $(selector). load() will execute an Ajax GET request and will set the content of the selected returned data (which should be either text or HTML).


1 Answers

This jquery forum thread sums it up:

$.post is a shorthand way of using $.ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code. $.get works on a similar principle.

—addyosmani

In short, this:

$.post( "/ajax", {"data" : json })  

Is equivalent to the following:

$.ajax({    type: "POST",    url: "/ajax",    data: {"data": json}  }); 
like image 193
rahul Avatar answered Oct 04 '22 02:10

rahul