Posted: 5/18/2010
Hello Experts,
$.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: '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.
1. POST vs GET
When we've been using POST and GET to pass data tru pages or sending to the server, GET method was avoid because it shows the form data on the URL string. So, it was totally unsecure.
But now, using AJAX, it doesnt show anything in the browsers URL because with AJAX we are sending only peace of website data to the server, which made totally NO DIFFERENCE between POST & GET in this manner.
Anyway, there is still another difference between these two methods. As the names refer, GET (to get something) and POST (to post something) means that when both methods were made, they were intended to be used for different purpose.
GET - you can use it to get data tru pages. So once you send data using GET method, these data are cached. If the same GET object is requested again, instead of new data it will use the cached ones. This is common mistake that beginners do when chosing which method to use, so that, afterwards they are confused when comming to such scenarios.
POST - you can use to POST data to the server so its expected to update these data info using the last POSTed data.
Another difference is in the size of data both method can transfer. GET method is easier to use, but cannot pass data size same as POST, it is quite limited.
2.
type - the method type you are using to send data to the server
dataType - what data type you want to call with this AJAX call. You can call text, xml, json, even script and evaluate responses from the server, so it will know what type of data expects from the server to return.
contentType - defines the file format of the website - more to read about this: http://en.wikipedia.org/wiki/Content-Type - bellow you have different content-types which will clear up your doubts.
Hope this helps,Hajan
Thanks Hajan.Its helpful