簡體   English   中英

ASP.NET PageMethod onSuccess函數返回HTML字符串

[英]ASP.NET PageMethod onSuccess function returns a HTML String

真奇怪 我的javascript文件中的onSuccess()函數返回了整個網頁的大量HTML代碼,而不是我實際上從C#WebMethod返回的值

1)我在Windows 10上運行Visual Studio 2015

2)調試后,我在SignupAccount C#方法中保留了一個斷點。 斷點沒有命中。 到那時還沒到。 但是調用了onSuccess函數

這是一個解釋:

ASP.NET Web窗體的HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
   <!--Code-->
 </head>
 <body class="account2 signup" data-page="signup">
   <!--Code-->
   <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

    <input type="text" name="firstname" id="firstname" placeholder="First Name" required autofocus>

    <input type="text" name="lastname" id="lastname" placeholder="Last Name" required autofocus>

    <button type="submit" onclick="signup()" id="submit-form" class="btn btn-lg btn-dark btn-rounded" data-style="expand-left">Sign In</button>

    <script src="JS/Account.js"></script>
   </form>
 </body>
</html>

C#代碼背后的WebMethod:

[WebMethod]
public static string SignupAccount(string fname, string lname)
{
    return "OK";
}

使用Javascript:

function signup() {
    var fname = document.getElementById("firstname").value;
    var lname = document.getElementById("lastname").value;

    PageMethods.SignupAccount(fname, lname, onSuccess, onFailure);

    function onSuccess(val) {
       alert(val);
       //Technically, this should display "OK". But instead it displays a HTML String 
       //of my entire Web Page starting from the <html> tag to the end
    }

    function onFailure() {}
}

為什么會這樣呢? 我相信程序和代碼是正確的。 這與Visual Studio有關嗎?

編輯

這是我從onSuccess函數獲得的響應

http://pastebin.com/6MjAFPY9

我在機器上創建了項目,工作代碼如下:

<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

    <asp:TextBox ID="firstname" runat="server" />
    <asp:TextBox ID="lastname" runat="server" />

    <asp:Button ID="btn" OnClientClick ="signup();return false;" runat="server" text="Call PageMethod" />

</form>

<script>
    function signup() {
        var fname = document.getElementById("firstname").value;
        var lname = document.getElementById("lastname").value;

        PageMethods.SignupAccount(fname, lname, onSuccess, onFailure);

        function onSuccess(val) {
            alert(val);
            //Technically, this should display "OK". But instead it displays a HTML String 
            //of my entire Web Page starting from the <html> tag to the end
        }

        function onFailure() { }
    }
</script>

后面的代碼:

[System.Web.Services.WebMethod]
    public static string SignupAccount(string fname, string lname)
    {
        return "OK";
    }

PS:確保頁面上沒有任何JavaScript錯誤。 希望這可以幫助!

我遇到了同樣的問題,這就是我解決的方法。 首先,從button標記中刪除onclick屬性:

<!-- before -->
<button type="submit" id="submit-form" onclick="signup()">Sign In</button>

<!-- after -->
<button type="submit" id="submit-form">Sign In</button>

然后將其替換為以下這行Java代碼:

$get('submit-form').addEventListener("click", function (event) {
    signup();
    event.preventDefault();
});

按鈕的click事件的默認事件處理程序將導致頁面提交,因此我們需要調用preventDefault [1] [2]來禁止此操作。

對我來說,問題出在我的App_Start/RouteConfig.cs

  1. 去掉
settings.AutoRedirectMode = RedirectMode.Permanent;
  1. 如果是routes.EnableFriendlyUrls(settings); 在其中,也可以在WebMethod調用之前將PageMethods.set_path添加到JavaScript文件中
...

PageMethods.set_path("YourPage.aspx");
PageMethods.SignupAccount(fname, lname, onSuccess, onFailure);

...

答案令人困惑:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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