C# 从javascript调用asp.net页面方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10658061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling asp.net page method from javascript not working
提问by windforceus
Hi I am calling a simple page method from javascript , here is my code at markup
嗨,我正在从 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" />
at cs
在 cs
[System.Web.Services.WebMethod]
public static string TestMethod(string para)
{
return "Yes this is working";
}
the alert show the result and it says "null". I check firebug and i don't see error from console.
警报显示结果,并显示“空”。我检查了萤火虫,但没有从控制台看到错误。
If i change the TestMethod to
如果我将测试方法更改为
[System.Web.Services.WebMethod]
public static string TestMethod()
{
return "Yes this is working";
}
And PageMethod to
和 PageMethod 到
PageMethods.TestMethod( function (response) { alert(response); } );
It shows the correct response as "Yes this is working". However, i need to pass parameter to the function. Do i miss anything?
它显示正确的响应为“是的,这是有效的”。但是,我需要将参数传递给函数。我想念什么吗?
Thanks for help.
感谢帮助。
采纳答案by RTigger
I think you have to use [ScriptMethod] instead of or in addition to [WebMethod] in order to have asmx methods available via javascript calls. The reason why it might work without taking a parameter is because the request doesn't have to parse anything in order to process the method.
我认为您必须使用 [ScriptMethod] 代替 [WebMethod] 或除了 [WebMethod] 之外,才能通过 javascript 调用获得 asmx 方法。它可以在不带参数的情况下工作的原因是因为请求不需要解析任何东西来处理方法。
Try it with [ScriptMethod] (and possibly [ScriptService] on your class definition) and see if that makes a difference.
尝试使用 [ScriptMethod](可能还有类定义中的 [ScriptService]),看看是否有所不同。
回答by Aymen
from what i remember, you just need 3 params in your call(your param, onsuccess and onfailure). did you try using PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError);
根据我的记忆,您在通话中只需要 3 个参数(您的参数、成功和失败)。您是否尝试使用 PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError);
回答by Shiv Mohan Saxena
I think the main problem is with the assembly you are using for ScriptManager.
我认为主要问题在于您用于 ScriptManager 的程序集。
<asp:ScriptManager ID="ScriptManager1"
EnablePageMethods="true"
runat="server" />
To resolve your problem use in Webconfig -
要解决您在 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>
and in your .aspx page use following lines -
并在您的 .aspx 页面中使用以下几行 -
<ajax:ScriptManager ID="ScriptManager1"
EnablePageMethods="true"
runat="server" />
Hope this will help you to resolve your problem.
希望这将帮助您解决您的问题。
回答by Ricardo Peres
The problem is that on your Web.config you need to have a module (IHttpModule) enabled: ScriptModule-4.0. This is enabled by default, but you may have removed it. Look for it in the machine-wide Web.config file, if you are curious, and see if it was removed from your local Web.config. Its declaration should be under system.webServer/modules (for IIS >= 7) and system.web/httpModules for Visual Studio's built-in web server or IIS < 7.
问题是在您的 Web.config 上,您需要启用一个模块 (IHttpModule):ScriptModule-4.0。默认情况下启用此功能,但您可能已将其删除。如果您好奇,请在机器范围的 Web.config 文件中查找它,看看它是否已从本地 Web.config 中删除。它的声明应该在 system.webServer/modules(对于 IIS >= 7)和 system.web/httpModules 对于 Visual Studio 的内置 Web 服务器或 IIS < 7。

