2012-12-11 49 views
0

它必須是一個經典問題,但我無法找到任何答案我的問題在線。 頁面位於

https://localhost/Secured/Report/FileWriterManager.aspx 

這裏是我的jQuery代碼。

par = 'some data'; 
    $.post(
      '/Secured/Report/FileWriterManager.aspx/CallAjax', 
      { strPar: par }, 
      function (msg) { 
       $('#lblError').text('Value Returned: ' + msg); 
      } 
     ); 

後面的代碼是一樣:

[WebMethod] 
public static string CallAjax(string strPar) 
{ 

    return 'OK'; 

} 

當我與Firefox調試,後聲明經過每行線,執行lblError填充。響應是整個FileWriterManager.aspx html代碼。我做錯了什麼?任何幫助將不勝感激。

+0

我可能是錯的,但我認爲你應該在你的javascript代碼中使用'PageMethods.CallAjax(par)'... – Vlad

+0

我試過pagemethod,它調用我的方法,但是PageMethods的問題是我可以不能訪問表單中的任何控件。 –

+0

你是什麼意思?該方法將返回一個可以放入控件的字符串。 'var s = PageMethods.CallAjax(par); $('#lblError')。text('Result:'+ s);'。或者我錯過了什麼? – Vlad

回答

0

您可以將Buffer="true"添加到FileWriterManager.aspx頁面上的<%# Page指令中,然後在此函數中調用代碼Response.Clear()來清除生成的HTML響應。然後使用Response.Write將結果生成到瀏覽器。

+0

感謝您的幫助,但它甚至不打電話給我的方法開始,所以添加響應清除不會改變我的問題。 –

0

如果在嘗試調用[WebMethod]時得到正常的頁面響應,應首先檢查ASP.NET Ajax是否配置正確。具體而言,請檢查web.config中的<HttpModules>部分。

對於ASP.NET 3.5應用程序,使用內置的Web服務器在Visual Studio:

<system.web> 
    <httpModules> 
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
    </httpModules> 
</system.web> 

當使用IIS:

<system.webServer> 
    <modules> 
    <remove name="ScriptModule"/> 
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
    </modules> 
</system.web> 

這樣做的原因,是該ScriptModule實際上是「攔截」/ WebForm.aspx/WebMethod請求,並處理靜態方法的調用和響應的返回。所以如果沒有發生,那麼模塊沒有配置。

如果您已經完成配置,請將頁面添加<asp:ScriptManager runat="server" EnablePageMethods="true"/>,並啓用頁面方法。然後測試您是否可以使用Firebug調用頁面方法:PageMethods.CallAjax("foo")