[英]Calling asp.net page method from javascript not working
嗨我從javascript調用一個簡單的頁面方法,這是我在標記處的代碼
function OnCallSumComplete(result, userContext, methodName) {
alert(result);
}
function OnCallSumError(error, userContext, methodName) {
if (error !== null) {
alert(error.get_message());
}
}
function test(){
var contextArray = "";
PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError, contextArray);
}
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
在cs
[System.Web.Services.WebMethod]
public static string TestMethod(string para)
{
return "Yes this is working";
}
警報顯示結果,並顯示“null”。 我檢查firebug,我沒有看到控制台的錯誤。
如果我將TestMethod更改為
[System.Web.Services.WebMethod]
public static string TestMethod()
{
return "Yes this is working";
}
和PageMethod到
PageMethods.TestMethod( function (response) { alert(response); } );
它顯示正確的響應為“是這是有效的”。 但是,我需要將參數傳遞給函數。 我什么都想念?
感謝幫助。
我認為主要問題在於您用於ScriptManager的程序集。
<asp:ScriptManager ID="ScriptManager1"
EnablePageMethods="true"
runat="server" />
要解決您的問題,請在Webconfig中使用 -
<pages>
<controls>
<add tagPrefix="ajax"
namespace="System.Web.UI"
assembly="System.Web.Extensions,
Version=1.0.61025.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
在.aspx頁面中使用以下行 -
<ajax:ScriptManager ID="ScriptManager1"
EnablePageMethods="true"
runat="server" />
希望這能幫助您解決問題。
我認為你必須使用[ScriptMethod]而不是[WebMethod],以便通過javascript調用獲得asmx方法。 它可能在沒有參數的情況下工作的原因是因為請求不必解析任何東西以便處理該方法。
嘗試使用[ScriptMethod](以及類定義中可能的[ScriptService]),看看是否有所作為。
問題是在Web.config上需要啟用模塊(IHttpModule):ScriptModule-4.0。 默認情況下啟用此功能,但您可能已將其刪除。 如果您感到好奇,請在機器范圍的Web.config文件中查找它,看看它是否已從本地Web.config中刪除。 它的聲明應該在system.webServer / modules(對於IIS> = 7)和system.web / httpModules下,用於Visual Studio的內置Web服務器或IIS <7。
從我記憶中,你只需要3個參數(你的param,onsuccess和onfailure)。 你嘗試過使用PageMethods.TestMethod(“test parameter”,OnCallSumComplete,OnCallSumError);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.