Difference between $.post and $.ajax?
The problem here is not the fact $.ajax()
is not working, it is because you did not set the type parameter in the Ajax request and it defaults to a GET request. The data is sent via the query string for get and if your backend expects them as post parameters, it will not read them.
$.post
is just a call with $.ajax()
, just with the type
set. Read the docs and you will see that $.ajax()
defaults to a GET as I mentioned above.
If you go to the jQuery.post page in the jQuery docs it shows you the $.ajax request with the type set. Again read the docs.
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}
});