C# PAGEMETHODS无法从JS函数运行

C# PAGEMETHODS无法从JS函数运行,c#,javascript,ajax,code-behind,pagemethods,C#,Javascript,Ajax,Code Behind,Pagemethods,我正在尝试使用pagemethods从JS函数调用代码隐藏方法,但它没有调用,也没有抛出任何错误 function example(){ pagemethods.method(); } **aspx.cs [webmethod] public static void method(){ //some logic } 所以为了找到问题,我做了一些负面测试 我评论了WEBMETHOD,然后它说“对象不支持此属性或方法”显示了一个错误。我可以假设这个案例显示pagemethods正在工作 然后

我正在尝试使用pagemethods从JS函数调用代码隐藏方法,但它没有调用,也没有抛出任何错误

function example(){

pagemethods.method();
}

**aspx.cs
[webmethod]
public static void method(){

//some logic
}
所以为了找到问题,我做了一些负面测试

  • 我评论了WEBMETHOD,然后它说“对象不支持此属性或方法”显示了一个错误。我可以假设这个案例显示pagemethods正在工作

  • 然后,我将JS函数中的调用方法名替换为pagemethods.newmethod(),但我没有将方法名更改为newmethod..我希望出现一些错误,但它没有给我错误

  • 注意:我在表单声明中有“method=post”。它对pagemethods有什么影响吗

    很困惑为什么会发生这个问题


    我们可以用任何其他方式调用codebehind方法而不是pagemethods吗..请给出建议

    也许您的aspx页面中缺少EnablePageMethods=true…

    在中,您可以看到此示例,因此您需要

    在标记中:

    <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
        <Scripts >
            <asp:ScriptReference Path="pm.js" />
        </Scripts>
    </asp:ScriptManager>
    
    更新
    另一种变体是对方法使用ajax请求,例如,在中使用jquery如下:

    function CallMethod(){
        $.ajax({
            type: "POST",
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            url: "YourPage.aspx/yourmathod",
            data:JSON.stringify({}), // parameters for method
            success: function (dt) { alert(dt);}, //all Ok
            error: function () { alert('error'); } // some error
        });
    }
    

    下面给出的代码对我有用

    .cs-

    using Microsoft.AspNet.FriendlyUrls;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        [System.Web.Services.WebMethod(EnableSession = true)] 
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static string yourmethod1()
        {
             return "Allow user";
        }
    }
    
    apsx.cs页面-

    <%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    
    <head runat="server">
    
        <title></title>
    
         <script type="text/javascript">
    
            function GreetingsFromServer()
            {
                PageMethods.yourmethod1(OnSuccess, OnError)
            }
            function OnSuccess(response)
            {
                alert(response);
            }
            function OnError(error) 
            {
                alert(error);
            }
        </script>
    
    </head>
    
    
    <body>
    
        <form id="form1" runat="server"  method="post" >
    
        <div>
                <asp:ScriptManager runat="server" EnablePageMethods="true"  EnablePartialRendering="true"  > </asp:ScriptManager>
    
                <input id="Button1" type="button" value="button" onclick=" return GreetingsFromServer();" />
    
        </div>
    
        </form>
    </body>
    </html>
    
    
    函数GreetingsFromServer()
    {
    PageMethods.yourmethod1(OnSuccess,OnError)
    }
    函数OnSuccess(响应)
    {
    警报(响应);
    }
    函数OnError(错误)
    {
    警报(错误);
    }
    
    Web.conf-

    <configuration>
    
      <appSettings>
               <add key="owin:AutomaticAppStartup" value="false" />
      </appSettings>
    
        <system.web>
    
                <compilation debug="true" targetFramework="4.5.2" />
    
                <httpRuntime targetFramework="4.5.2" />
    
                <authorization>
                           <allow users="*"/>
                </authorization>
    
        </system.web>
    
    </configuration>
    

    这种方法也有效-

    .aspx文件-

    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <%@ Import Namespace="Microsoft.AspNet.FriendlyUrls" %>
    <%@ Import Namespace="Newtonsoft.Json" %>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
    
        <script type="text/javascript">
    
            function CallMethod()
            {
                debugger;
    
    
                    jQuery.ajax({
    
                        url: 'Default.aspx/yourmethod',
                        type: "GET",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({}), // parameters for method
                        success: function (dt)
                        {
                            debugger;
                            alert('success : ' + dt.d  );
    
                        }, //all Ok
                        error: function (dt) {
                            debugger;
                            alert('error : ' + dt);
                        } // some error
    
                    });
    
    
            }
    
            function GreetingsFromServer()
            {
                PageMethods.yourmethod1(OnSuccess, OnError);
            }
            function OnSuccess(response)
            {
                debugger;
                alert(response);
            }
            function OnError(error) {
                alert(error);
            }
    
    
        </script>
    
    
    <script language="C#" runat="server">
    
        [System.Web.Services.WebMethod(EnableSession = true)]  
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static string yourmethod()
        {
            var settings = new Microsoft.AspNet.FriendlyUrls.FriendlyUrlSettings();
            settings.AutoRedirectMode = Microsoft.AspNet.FriendlyUrls.RedirectMode.Off;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject("Allow user");
            return json;
        }
    
    
         [System.Web.Services.WebMethod(EnableSession = true)]  
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
          public static string yourmethod1()
        {
            //string json = Newtonsoft.Json.JsonConvert.SerializeObject("Allow user");
            // or
            return "Allow user";
        }
    
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server" >
            <div>
    
                <asp:ScriptManager runat="server" EnablePageMethods="true"  EnablePartialRendering="true"  > </asp:ScriptManager>
                <input id="Button1" type="button" value="button" onclick="return GreetingsFromServer();" />
                 <input id="Button2" type="button" value="button" onclick="return CallMethod();" />
            </div>
        </form>
    </body>
    </html>
    
    
    函数CallMethod()
    {
    调试器;
    jQuery.ajax({
    url:'Default.aspx/yourmethod',
    键入:“获取”,
    contentType:“应用程序/json;字符集=utf-8”,
    数据类型:“json”,
    数据:JSON.stringify({}),//方法的参数
    成功:功能(dt)
    {
    调试器;
    警报(“成功:”+dt.d);
    },//一切正常
    错误:函数(dt){
    调试器;
    警报('错误:'+dt);
    }//有些错误
    });
    }
    函数GreetingsFromServer()
    {
    PageMethods.yourmethod1(OnSuccess,OnError);
    }
    函数OnSuccess(响应)
    {
    调试器;
    警报(响应);
    }
    函数OnError(错误){
    警报(错误);
    }
    [System.Web.Services.WebMethod(EnableSession=true)]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet=true,ResponseFormat=System.Web.Script.Services.ResponseFormat.Json)]
    公共静态字符串方法()
    {
    var settings=new Microsoft.AspNet.FriendlyUrls.FriendlyUrlSettings();
    settings.AutoRedirectMode=Microsoft.AspNet.friendlyurl.RedirectMode.Off;
    字符串json=Newtonsoft.json.JsonConvert.SerializeObject(“允许用户”);
    返回json;
    }
    [System.Web.Services.WebMethod(EnableSession=true)]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet=true,ResponseFormat=System.Web.Script.Services.ResponseFormat.Json)]
    公共静态字符串yourmethod1()
    {
    //字符串json=Newtonsoft.json.JsonConvert.SerializeObject(“允许用户”);
    //或
    返回“允许用户”;
    }
    
    Web.conf-

    <configuration>
    
      <appSettings>
               <add key="owin:AutomaticAppStartup" value="false" />
      </appSettings>
    
        <system.web>
    
                <compilation debug="true" targetFramework="4.5.2" />
    
                <httpRuntime targetFramework="4.5.2" />
    
                <authorization>
                  <allow users="*"/>
                </authorization>
    
        </system.web>
    
    </configuration>
    
    
    
    为什么您认为它没有调用?试试看,我正在调试带有断点的代码,所以我没有看到调用过使用pagemethods调用的静态方法。我使用的概念与“在ASP.NET网页中调用静态方法”中提到的相同你能提供你的标记吗?我可以在示例项目中使用pahemethods调用方法,但当我在实际项目中使用相同的逻辑时,它不起作用。你能告诉我有没有其他方法可以从javascript调用这些方法。谢谢你的回复。如果我不包括pagemethods本身给我的属性,我就启用了该属性错误..你能告诉我有没有其他方法可以从javascript调用这些方法。。
    <configuration>
    
      <appSettings>
               <add key="owin:AutomaticAppStartup" value="false" />
      </appSettings>
    
        <system.web>
    
                <compilation debug="true" targetFramework="4.5.2" />
    
                <httpRuntime targetFramework="4.5.2" />
    
                <authorization>
                  <allow users="*"/>
                </authorization>
    
        </system.web>
    
    </configuration>