简体   繁体   English

PAGEMETHODS 不适用于 JS 函数

[英]PAGEMETHODS is not working from JS function

I am trying to call code behind method from JS function using pagemethods but its not calling and its not throwing any error either...我正在尝试使用 pagemethods 从 JS 函数调用方法背后的代码,但它没有调用,也没有抛出任何错误......

function example(){

pagemethods.method();
}

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

//some logic
}

so to find the issue i did some negative testing for that所以为了找到这个问题,我做了一些负面测试

  1. I commented WEBMETHOD then it showed an error by saying"object does not support this property or method".can i assume this case shows pagemethods is working!!!我评论了 WEBMETHOD 然后它通过说“对象不支持此属性或方法”来显示错误。我可以假设这种情况表明 pagemethods 正在工作!!!

  2. Then I replaced calling method name in JS function to pagemethods.newmethod() but i didn't change method name to newmethod..i was expecting an some error but it didn't give me an error..然后我将 JS 函数中的调用方法名称替换为 pagemethods.newmethod() 但我没有将方法名称更改为 newmethod..我期待一些错误,但它没有给我一个错误..

NOTE:i have "method=post" in form declaration..does it effect pagemethods anything..注意:我在表单声明中有“method=post”..它会影响 pagemethods 吗..

so confused why this issue is happening!!!很困惑为什么会发生这个问题!!!

can we call codebehind method in any other way instead of pagemethods..please advice!!!我们可以以任何其他方式调用代码隐藏方法而不是 pagemethods..请建议!!!

in msnd you can see a sample of this, so you need...msnd 中,您可以看到此示例,因此您需要...

in markup:在标记中:

<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
    <Scripts >
        <asp:ScriptReference Path="pm.js" />
    </Scripts>
</asp:ScriptManager>

in code behind: your static method, with attribute [WebMethod]在后面的代码中:您的静态方法,具有[WebMethod]属性

in pm.js : something like thispm.js :像这样

function example(){
    PageMethods.method();
}

UPDATE更新
Another variant is use an ajax request to your method, for example with jquery in goes like this:另一个变体是对您的方法使用 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
    });
}

也许您的 aspx 页面中可能缺少 EnablePageMethods = true ...

Below given code worked for me.下面给定的代码对我有用。

.cs - 。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 - 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>

Web.conf - 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>

This approach is also working -这种方法也有效——

.aspx file - .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>

Web.conf - 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>

暂无
暂无

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

相关问题 从iframe中的页面检索会话的PageMethods无法正常工作 - PageMethods to retrieve session from page in iframe not working 从Java中的PageMethods成功函数返回结果 - Return result from PageMethods success function in Javascript PageMethods不起作用,因为方法未从javascript调用 - PageMethods not working because method is not calling from javascript 使用PageMethods从Javascript代码调用C#布尔函数 - Call C# boolean function from Javascript code using PageMethods JavaScript中的PageMethods作为函数的返回 - PageMethods in javascript as return of a function 使用 PageMethods 时不调用 function - Not calling function when using PageMethods 从代码隐藏和代码隐藏中的脚本调用函数内部调用JavaScript(PageMethods是unfind) - Call JavaScript from codebehind and inside the script call function in codebehind (PageMethods is undefind) 使用ASP.Net中的AJAX PageMethods从数据库中获取JQuery AutoComplete TextBox在Internet Explorer中不起作用 - JQuery AutoComplete TextBox from database using AJAX PageMethods in ASP.Net is not Working in Internet Explorer 从具有多个参数的 PageMethods C# 返回值后不调用成功函数 - Not Calling Success Function After Returning Values from PageMethods With Multiple Parameters C# 在不使用脚本管理器或页面方法的情况下,从asp.net 2.0中的javascript调用服务器端函数 - Call a server side function from javascript in asp.net 2.0 without using script manager or pagemethods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM