jquery的post()方法是否能够调用asp.net 3.5 webmethod?

mik*_*ev2 5 asp.net ajax jquery webmethod

这是一些javascript:

$.ajax({
        type: "POST",
        url: "default.aspx/GetDate",
        contentType: "application/json; charset=utf-8",
        data: {},
        dataType: "json",
        success: function(result) {
            alert(result.d);
        }
     });
Run Code Online (Sandbox Code Playgroud)

上面的方法可以正常工作,并在default.aspx中警告从[WebMethod]返回的名为GetDate的字符串

但是当我使用时:

$.post(
        "default.aspx/GetDate",
        {},
        function(result) {
            alert(result.d);
        },
        "json"
     );
Run Code Online (Sandbox Code Playgroud)

此成功方法中的警报永远不会触发.

在firebug中我可以看到POST基本上有效 - 它返回200 OK
但是在这种情况下的响应是整个default.aspx页面的HTML而不是我使用$ .ajax()方法时返回的JSON.

编辑:
firebug中显示的响应和请求标头不相同.

使用$ .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
Run Code Online (Sandbox Code Playgroud)

使用$ .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
Run Code Online (Sandbox Code Playgroud)

我可以使用$ .post()方法,还是必须使用$ .ajax()方法?

Dar*_*rov 3

这很正常。当您使用时,$.post您不能contentType: 'application/json'像使用 那样进行设置$.ajax。服务器需要这个标头。因此基本上您不能使用$.post.