What is the difference between those two AJAX calls and why would I choose to use either when using the asp.net mvc framework?
get
is an HTTP GET call, and post
is an HTTP POST.
See Wikipedia for more explanation.
Essentially, if you are including data (such as with a form submission), use post
; otherwise if you are just loading a page, or you want to pass query string parameters, use get
. (any data you pass to get
will be passed as query string)
edit:
They are separated because the underlying XMLHttpRequest object (the fundamental AJAX object in browsers) requires specifying a method. In fact, even lower than that, when you request a webpage you have to either HTTP/1.0 GET or HTTP/1.0 POST (or the other request methods). If you are talking from a design point of view, as an alternative to passing a string into some sort of "load" method, I can't say what the software architects behind jQuery were thinking.
edit2:
Actually if you look at the latest jQuery documentation, get and post are listed under "shorthand methods" and equivalent methods are given. So, they are just for shortening your code and are synonymous to the ajax method.
One uses POST and one uses GET.
As far as what they're meant for - the only real technical difference (please correct this post if I'm wrong) is that GET has a much shorter limit to the query string. In practice, GET is meant for when fetching something from the server. A GET call should not cause side effects on the server. POST is when you intend to send something on the server and have it do something with it.
edit: the word I was looking for, to describe GET, is idempotent. You should be able to make the exact same GET call an unlimited number of times, and get the same result every time, with no consequence to the server (provided of course that no one else has changed the server's state.) But remember that there are no technical barriers preventing you from misusing either GET or POST.
$.post
and $.get
are just wrappers for $.ajax
with respective values for the type
parameter. (Lines 4888 and 4913 in the source.)
In MVC, you can specify that a controller action only accept requests made using a certain HTTP request type (using the AcceptVerbs controller/action attribute).
If you were submitting a request to such an action, you would need to specify the request type, in which case you might find $.post
or $.get
a handy shortcut to $.ajax
.
You should always use POST when pushing change sets - there are security implications, I will leave all the hory details of that to research.