Asp.net

使用 jQuery 呼叫 ASP.NET PageMethod/WebMethod - 返回整個頁面

  • February 24, 2009

jQuery 1.3.2,ASP.NET 2.0。對 PageMethod (WebMethod) 進行 AJAX 呼叫會返回完整/整個頁面,而不僅僅是響應。page 方法上的斷點表明它永遠不會被命中。我的方法上有 [WebMethod] 屬性,它公共靜態的,返回字元串並且不接受任何參數。我什至嘗試在我的班級頂部添加 [ScriptService] 以查看它是否有幫助,但它沒有。

我已經看到這篇文章Jquery AJAX with ASP.NET WebMethod Returning Entire Page有我相同的症狀,但我仍然遇到問題。我讀了<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);
   }
});

根據 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; 

我在我的頁面中添加了一個 ScriptManager 只是為了看看它是否有幫助,但那裡沒有運氣。

有什麼建議?

你知道頁面方法工作正常嗎?如果您使用 ScriptManager,它們會起作用嗎?

聽起來您可能缺少web.config 條目。特別是 HttpModules 部分。

引用自:https://stackoverflow.com/questions/583116