When I run an ajax request of type “POST”, the parameters are not included in the string.
This code:
$telerik.$.ajaxSetup({
accepts: 'application/json, text/javascript, */*'
});
var parameters = {
"playerId": args.playerId
};
var ajaxCallParameters = {
async: true,
cache: false,
url: "../Services/CmsWebService.svc/SendUpdateRequestToPlayer",
type: "POST",
data: parameters,
dataType: 'json',
error: function (jqXHR, textStatus, errorThrown) {
var errorString = 'Error thrown from ajax call: ' + textStatus + 'Error: ' + errorThrown;
var displayPanel = document.getElementById('requestStatusUpdateResults');
$telerik.$(displayPanel).text(errorString);
},
success: function (data, textStatus, jqXHR) {
var displayPanel = document.getElementById('requestStatusUpdateResults');
$telerik.$(displayPanel).text(data.d);
}
};
$telerik.$.ajax(ajaxCallParameters);
Triggers this request (viewed in Fiddler)
POST /Web/Services/CmsWebService.svc/SendUpdateRequestToPlayer HTTP/1.1
As you can see, the parameter is not included.
If I simply change the word “POST” to “GET” in the code then this is the request:
GET /Web/Services/CmsWebService.svc/SendUpdateRequestToPlayer?playerId=1&_=1320213556288 HTTP/1.1
What’s going on? What it is about POST which stops request parameters being included?
HTTP POST behaves different than GET. When you use HTTP GET, it sends the parameters as part of the URL’s query string, which is why you see them added on. However, when you use HTTP POST, the parameters are sent as part of the HTTP Body and therefore they aren’t added to the URL.
The reason for using POST instead of GET is that GET parameters have a size limit in some browsers, whereas you can have parameters of almost any length when submitted via a POST.
Depending on what backend server technology is being used, the server may treat GET parameters and POST parameters the same and therefore your backend service will handle either one fine. However, in a lot of cases you have to specifically code your backend service to accept parameters via GET & POST.