difference between AJAX POST and GET
$.ajax({
type: 'POST',
url: path,
data: '{AreaID: ' + parentDropdownList.val() + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(response)
{
}
});
In above code I am using type开发者_如何学C: 'POST'. My senior told me that I also can use 'GET' in type. But dint find the difference between 'POST' and 'GET' and I also want to know what is the use of type, contentType, and dataType.
Could anyone one explain me why we use these type, contentType and dataType.
Thanks in advance.
These are some of the fundamentals of web communication:
Have a read here: http://javascript.about.com/od/ajax/a/ajaxgp.htm
Essentially GET creates a query string (www.mysite.co.uk/mypage?id=1%name=john%something=anothervalue etc.etc.). This means that it is possible to invoke a GET request directly from the URL on a browser. Web servers actually cache the results of GET requests for performance reasons. There are very much designed for data retrieval.
POST actually sends the data directly to the server and the result is never cached.
I'd always suggest using something like Firebug for Firefox or the Web Development Helper for IE so that you can see the data transfer between client and server.
As a rule of thumb, use GET to retrieve data and POST to update it.
Also, see a great answer to the same question here: GET vs POST in AJAX?
David is Correct. One additional word of warning when choosing between GET and POST is to realise that GET will be cached by browsers like IE rather than being called every time, whereas POST (when data is included) will not be cached.
GET:
1) Data is appended to the URL(QueryString)
2) Data is not secret.(Can be seen by anyone)
3) It is a single call system
4) Maximum data that can be sent is 256.
5) Data transmission is faster
6) This is the default method for many browsers
POST:
1) Data is not appended to the URL but sent as part of Http Body.
2) Data is Secret
3) It is a two call system.
4) There is no Limit on the amount of data.That is characters any amount of data can be sent.
5) Data transmission is comparatively slow.
6) No default and should be Explicitly specified.
精彩评论