开发者

jQuery and AJAX what should I use?

开发者 https://www.devze.com 2022-12-24 07:06 出处:网络
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...

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 u开发者_StackOverflowse POST and JSON to connect to CodeIgniter PHP framework.

Thank you.


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)


Use $.ajax and always, always, always include the error parameter (which the shorthand $.post and $.get inexplicably leave out — sometimes short is a bit too short). Unless you live in a perfect world where nothing ever goes wrong, in which case, where do I sign? :-)


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


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" });


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号