views:

126

answers:

7

Hello,

I've decided to go with jQuery for all my AJAX related client side needs. But jQuery has way too many functions for the same task: $.post, $.get, $.ajax, $.getJSON... My question is what should I use?

EDIT: I'm going to use POST and JSON to connect to CodeIgniter PHP framework.

Thank you.

+3  A: 

Most of them are just short hand for $.ajax which is generally what I use.

matpol
+2  A: 

No the question is what do you want to do?

If you send back data in a format that is not JSON there's no reason to use the $.getJSON.

$.ajax is more customizable but it's more complex then the other functions.

I usually choose between $.post and $.get.

mck89
+2  A: 

$.post and $.get and $.getJSON are all convenience methods that wrap $.ajax. The ones you choose are just a matter of style.

BC
Seems kind of weird that all this for a simple style decision.
thedp
It's not about style, it's about the need. The need is dictated by the service end of the ajax solution. It's part of the contract wether http POST or http GET should be used. And if you're using `.get` to retrieve data that happens to be JSON, `$.getJSON` is a nice convenience method
Sander Rijken
if it was about need there would be no general purpose $.ajax function.
BC
@BC hit the exact point of my question, why do you need `$.ajax` then?
thedp
Consistency? Generalization? (style?)
BC
@BC: I meant that you hit the point with the comment, that it's not really about purpose, because why then would we need `$.ajax` ?!
thedp
+3  A: 

It really depends on the situation.

However, if you're using ASP.Net with Web Services, you probably will want to use the $.ajax method as you need to pass an empty data set.

This blog post explains why:

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

An example:

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService.asmx/WebMethodName",
  data: "{}",
  dataType: "json"
});

As you can see, the $.ajax() method allows you to specify "GET" ( passed in the query string ) or "POST" ( passed in the request )

Yes, it's more "complex" but you can use $.ajaxSetup() to simplify the call:

Here's a blog post on that: http://encosia.com/2009/07/21/simplify-calling-asp-net-ajax-services-from-jquery/

An example from that page:

$.ajaxSetup({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: "{}"
});

This sets up defaults for the values in $.ajax so you don't need to set them up.

Your code could then be as simple as:

$.ajax({
   url: "HelloWorld.asmx/Hello",
   success: function(msg) {
      /* Do Stuff */
  }
});

or even:

$.ajax({ url: "HelloWorld.asmx/Hello" });
Atømix
+6  A: 

It really depends, it is not AJAX or jQuery specifc, it's about HTTP protocol.

POST method should be used to send data which is submited once, like a form. POST & GET method doesn't have the same lenght limit, you can use POST to send more data than with GET.

GET is used to 'get' a static page. (albeit it is not really true but anyway, you get the point)

For example, when a form is submited, POST method is used because the result content is specific to this request.

Also you have to note that POST will never be cached by your browser or jQuery.

$.post & $.get are just proxy method to $.ajax(options);

and $.getJSON is specially designed to handle JSON result with GET method only (thanks to thedp)

Boris Guéry
just wanted to add a note: `$.getJSON` is to handle JSON under GET only.
thedp
+4  A: 
T.J. Crowder
I totally forgot about error handling here. Thank you! :)
thedp
+1  A: 

The documentation for short hand methods like $.get() and $.post() describe what the $.ajax() equivalent is.

For example get describes:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

Short story: read the docs for what does what, and what meets your needs

Sander Rijken
I did read, and that what confuses me. Why should I $.ajax and not $.post and the other way around.
thedp
sometimes `$.post` or `$.get` isn't flexible enough. Last time I needed `$.ajax` I needed the get request to be synchronous, where `$.get` is async.
Sander Rijken