使用jQuery调用ASP.NET PageMethod/WebMethod - 返回整个页面

Mat*_*att 36 asp.net jquery web-services asmx pagemethods

jQuery 1.3.2,ASP.NET 2.0.对PageMethod(WebMethod)进行AJAX调用会返回完整/整个页面,而不仅仅是响应.页面方法上的断点显示它永远不会被击中.我的方法有[WebMethod]属性,它 public static,返回string并且不接受params.我甚至尝试在我的班级顶部添加[ScriptService]以查看它是否有帮助,但事实并非如此.

我已经看到这篇帖子Jquery AJAX与ASP.NET WebMethod返回整个页面有相同的症状,但我仍然有问题.我读了http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/,我觉得我跟着这个到了T,但仍然没有运气.

我正在制作的jQuery调用是:

jQuery.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: '{}',
    dataType: 'json',
    url: 'MyPage.aspx/SomePageMethod',
    success: function(result){
        alert(result);
    }
});
Run Code Online (Sandbox Code Playgroud)

根据FF3中的Firebug,请求/响应头如下所示

Response Headers
Server  ASP.NET Development Server/8.0.0.0
Date    Tue, 24 Feb 2009 18:58:27 GMT
X-AspNet-Version    2.0.50727
Cache-Control   private
Content-Type    text/html; charset=utf-8
Content-Length  108558
Connection  Close

Request Headers 
Host    localhost:2624
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept  application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  300
Connection  keep-alive
Content-Type    application/json; charset=utf-8
X-Requested-With XMLHttpRequest
Referer http://localhost:2624/MyApp/MyPage.aspx
Content-Length  2
Cookie  ASP.NET_SessionId=g1idhx55b5awyi55fvorj055; 
Run Code Online (Sandbox Code Playgroud)

我已经在我的页面中添加了一个ScriptManager,只是为了查看它是否有帮助,但没有运气.

有什么建议?

Dav*_*ard 27

你知道Page Methods正常工作吗?如果您使用ScriptManager,它们是否有效?

听起来您可能缺少web.config条目.特别是HttpModules部分.

  • 嗨戴夫.根据@Surya,该链接似乎不再起作用. (3认同)

小智 23

我在web.config中缺少一行:

<system.web>
  <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </httpModules>
</system.web>
Run Code Online (Sandbox Code Playgroud)

  • Adam Seabridge,你现在在哪里,我爱你. (9认同)

Mat*_*att 13

我今天再次遇到这个问题的原因不同:我拼错了"应用程序"

contentType: 'application/json'
Run Code Online (Sandbox Code Playgroud)

并且获得了整页响应,而不是调用WebMethod.


Mar*_*ado 6

如果您已经尝试了所有这些并仍然从页面方法返回整个页面,您可能需要确保您没有使用友好的网址.如果您正在使用它们,这个技巧可能对您有所帮助

在进行调用之前,在js脚本中添加以下行:

PageMethods.set_path(PageMethods.get_path() + '.aspx');
Run Code Online (Sandbox Code Playgroud)