views:

412

answers:

5

What is the difference between those two AJAX calls and why would I choose to use either when using the asp.net mvc framework?

+1  A: 

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.

Ricket
I know how the normal ones work but in terms of Jquery, I dont get why they are seperate.
Shawn Mclean
+4  A: 

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.

Tesserex
A Get can cause side effects, it should just be idempotent. See http://en.wikipedia.org/wiki/Idempotent#In_computing. (Not causing side effects is 1 way to be idempotent, but not the only way).
Kyle Butt
Thank you! "Idempotent" was exactly the word I was looking for while writing that, I just couldn't remember it.
Tesserex
+2  A: 

$.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.

Lobstrosity
+1  A: 

The one gotcha I've run into is that $.get calls are cached by IE, which is a problem given you're usually using Ajax to refresh data. If you want to use GET but don't want to be cached, you have to drop down to $.ajax and pass cache: false in the settings.

Tom
A: 

You should always use POST when pushing change sets - there are security implications, I will leave all the hory details of that to research.

Mark Schultheiss