Here's some javascript:
$.ajax({
type: "POST",
url: "default.aspx/GetDate",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
success: function(result) {
alert(result.d);
}
});
The above method works as I would expect and alerts the string returned from the [WebMethod] called GetDate in default.aspx
But when I use:
$.post(
"default.aspx/GetDate",
{},
function(result) {
alert(result.d);
},
"json"
);
The alert in this success method never fires.
In firebug I can see that the POST has basically worked - it returns 200 OK
But the response in this case is the HTML of the entire default.aspx page rather than the JSON returned when I use the $.ajax() method.
EDIT:
The response and request headers shown in firebug are NOT identical.
With $.ajax()...
REQUEST:
Accept application/json, text/javascript, */*; q=0.01
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language en-gb,en;q=0.5
Connection keep-alive
Content-Type application/json; charset=utf-8
Cookie (removed)
Host (removed)
Referer (removed)
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
X-Requested-With XMLHttpRequest
RESPONSE:
Cache-Control private, max-age=0
Content-Length 27
Content-Type application/json; charset=utf-8
Date Wed, 11 Jan 2012 12:36:56 GMT
Server Microsoft-IIS/7.5
X-Powered-By ASP.NET
With $.post()...
REQUEST:
Accept application/json, text/javascript, */*; q=0.01
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language en-gb,en;q=0.5
Connection keep-alive
Cookie (removed)
Host (removed)
Referer (removed)
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
X-Requested-With XMLHttpRequest
RESPONSE:
Cache-Control private
Content-Length 13815
Content-Type text/html; charset=utf-8
Date Wed, 11 Jan 2012 12:36:53 GMT
Server Microsoft-IIS/7.5
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
Can I use the the $.post() method for this, or do I have to use the $.ajax() method?
That's normal. When you use $.post
you cannot set contentType: 'application/json'
as you do with $.ajax
. And the server expects this header. So basically you cannot invoke an ASP.NET page method with $.post
.
I presume this is because your serverside code demands the Content-Type
header to be set. The default implementation of $.post
does not allow this.
If, however, every single time you use AJAX in your application you need this header to be present, you could modify $.post
:
$.post = function (url, data, callback, type) {
if (jQuery.isFunction(data)) {
type = type || callback;
callback = data;
data = undefined;
}
return jQuery.ajax({
type: 'POST',
url: url,
data: data,
success: callback,
dataType: type
contentType: "application/json; charset=utf-8"
});
};
With this, all $.post
calls will have contentType
set. This may be a good idea; it may not...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With