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
?
$. 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.
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.
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.
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).
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} });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With