Is jquery post () method able to call asp.net 3.5 web method?

Here are 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 expected and warned the string returned from [WebMethod] called GetDate in default.aspx

But when I use:

$.post(
        "default.aspx/GetDate",
        {},
        function(result) {
            alert(result.d);
        },
        "json"
     );

A warning in this success method never fires.

In firebug, I see that POST basically worked - it returns 200 OK
But the answer in this case is the HTML of the entire default.aspx page, and not the returned JSON 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

Is it possible to use the $ .post () method for this, or do I need to use the $ .ajax () method?

+5
source share
2

. $.post, contentType: 'application/json', , $.ajax. . , ASP.NET $.post.

+3

, , serveride Content-Type. $.post .

, , , AJAX , , $.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"
    });
};

$.post contentType. ; ...

+3

All Articles