调用asp.net页面的方法从JavaScript不工作 [英] Calling asp.net page method from javascript not working

查看:132
本文介绍了调用asp.net页面的方法从JavaScript不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从JavaScript调用一个简单的页面的方法,这里是我的code。在标记

Hi I am calling a simple page method from javascript , here is my code at markup

 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";
 }

该警报显示结果和它说空。我检查萤火虫,我没有看到错误的控制台。

the alert show the result and it says "null". I check firebug and i don't see error from console.

如果我的TestMethod的改变。

If i change the TestMethod to

 [System.Web.Services.WebMethod]
 public static string TestMethod()
 {
    return "Yes this is working";
 }

和PageMethod的,以

And PageMethod to

 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?

感谢您的帮助。

推荐答案

我觉得你有才能有通过JavaScript调用可用的ASMX方法使用[ScriptMethod]代替或补充[的WebMethod]。为什么它可能不以参数工作的原因是因为该请求没有以处理方法来分析任何

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]试试吧(也可能是[ScriptService]在你的类定义),看看是否有差别。

Try it with [ScriptMethod] (and possibly [ScriptService] on your class definition) and see if that makes a difference.

这篇关于调用asp.net页面的方法从JavaScript不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆