您好,我正在从 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" />
希望这能帮助您解决问题。
关于c# - 从 javascript 调用 asp.net 页面方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658061/