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?
Content-Type
header not vary?