简体   繁体   English

从javascript调用asp.net页面方法无法正常工作

[英]Calling asp.net page method from javascript not working

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". 警报显示结果,并显示“null”。 I check firebug and i don't see error from console. 我检查firebug,我没有看到控制台的错误。

If i change the TestMethod to 如果我将TestMethod更改为

 [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. 感谢帮助。

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. 希望这能帮助您解决问题。

I think you have to use [ScriptMethod] instead of or in addition to [WebMethod] in order to have asmx methods available via javascript calls. 我认为你必须使用[ScriptMethod]而不是[WebMethod],以便通过javascript调用获得asmx方法。 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. 它可能在没有参数的情况下工作的原因是因为请求不必解析任何东西以便处理该方法。

Try it with [ScriptMethod] (and possibly [ScriptService] on your class definition) and see if that makes a difference. 尝试使用[ScriptMethod](以及类定义中可能的[ScriptService]),看看是否有所作为。

The problem is that on your Web.config you need to have a module (IHttpModule) enabled: ScriptModule-4.0. 问题是在Web.config上需要启用模块(IHttpModule):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. 如果您感到好奇,请在机器范围的Web.config文件中查找它,看看它是否已从本地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. 它的声明应该在system.webServer / modules(对于IIS> = 7)和system.web / httpModules下,用于Visual Studio的内置Web服务器或IIS <7。

from what i remember, you just need 3 params in your call(your param, onsuccess and onfailure). 从我记忆中,你只需要3个参数(你的param,onsuccess和onfailure)。 did you try using PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError); 你尝试过使用PageMethods.TestMethod(“test parameter”,OnCallSumComplete,OnCallSumError);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM