簡體   English   中英

PAGEMETHODS 不適用於 JS 函數

[英]PAGEMETHODS is not working from JS function

我正在嘗試使用 pagemethods 從 JS 函數調用方法背后的代碼,但它沒有調用,也沒有拋出任何錯誤......

function example(){

pagemethods.method();
}

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

//some logic
}

所以為了找到這個問題,我做了一些負面測試

  1. 我評論了 WEBMETHOD 然后它通過說“對象不支持此屬性或方法”來顯示錯誤。我可以假設這種情況表明 pagemethods 正在工作!!!

  2. 然后我將 JS 函數中的調用方法名稱替換為 pagemethods.newmethod() 但我沒有將方法名稱更改為 newmethod..我期待一些錯誤,但它沒有給我一個錯誤..

注意:我在表單聲明中有“method=post”..它會影響 pagemethods 嗎..

很困惑為什么會發生這個問題!!!

我們可以以任何其他方式調用代碼隱藏方法而不是 pagemethods..請建議!!!

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

在標記中:

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

在后面的代碼中:您的靜態方法,具有[WebMethod]屬性

pm.js :像這樣

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

更新
另一個變體是對您的方法使用 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 ...

下面給定的代碼對我有用。

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

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>

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM