开发者

jquery GET and POST confusion

I am not quiet sure how jquery works. I want t开发者_StackOverflow社区o know few things about the GET and POST in terms of jQuery.

I use the following code in my app :

<script>
function example_ajax_request() {
  $('#example-placeholder').html('<p>Loading results ... <img src="ajax-loader.gif"  /></p>');
  $('#example-placeholder').load("ind.php?show=" + $('#box option:selected').val());
}
</script>

I am not using either GET or POST in this method. I am using a button to call the example_ajax_request to process the request and get the results.

Sometimes I see code like this:

$.ajax({
  url: 'loader.php',
  data: 'somedata',
  method: 'GET',
  success: function(data){
    $('#er').text(data);
  }
});

My doubt is, is it required or not that we use a method to post the data? (either GET or POST) while send and getting the data in PHP webapps? The code that I use first works fine even though I do not use any POST or GET methods.

Any inputs?

Thanks.


"Read the manual" - http://api.jquery.com/load/

The POST method is used if data is provided as an object; otherwise, GET is assumed.


All data you send to a server from a browser (in the normal case) will be via HTTP verbs (request methods), almost always GET or POST although there are others. GET is what browsers send to servers when you want to visit a web page, for instance. POST is almost always what's used to send forms to servers, for various reasons.

jQuery ajax methods can use GET or POST depending on what you tell them to do. load actually decides for you based on the data you tell it to send with the request. jQuery's jQuery.ajax, jQuery.get, and jQuery.post functions let you say specifically what you want to do.


Using .load() in the way you did, it requests the resource via GET. By appending parameters to the URL (?param1=value1) you are providing "GET parameters".

This:

$.ajax({
  url: 'loader.php',
  data: 'param1=value1',
  method: 'GET',
  success: function(data){
    $('#er').text(data);
  }
});

is basically the same as

$('#er').load('loader.php?param1=value');

The advantage of the first method is, among others, that you can do a lot more inside the callback function (success :function(){}) than just putting the content into an element. You gain more control. Read about .ajax().


The differences between GET and POST is that using the GET method, parameters are sent as part of the URL whereas with POST, parameters are sent in the body of the request. Thus it is easier to provide parameters via GET. On the other hand, URLs cannot be of arbitrary length so sending parameters via GET is limited.

GET should be used if you really only want to get data. Parameters can be used to control the output (e.g. this is used when you paginate are result set).

POST on the other hand should be used if you want to change data on the server side.

Read more about the request methods on Wikipedia.


It's always a good idea to read the manual. http://api.jquery.com/load/

"The POST method is used if data is provided as an object; otherwise, GET is assumed."


Both .load() and .ajax() do GET request by default. The result is often the same, but in order to be a good developer you have to understand the differences and the philosophy for each of them. Get seems to be easier to use at is more transparent in terms of passed information. You pass visible parameters. POST allows bigger and more complex information to be passed.

In terms of vocabulary, the purists say that GET should GET information and POST should POST it.

Advice: GET requests are cacheable by browser so add extra param with timestamp to make the content refresh.

As a reccomandation I would use ajax() with POST as ajax is a complex request that allows you to handle all errrors.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜