ASP.NET PageMethod onSuccess函数返回一个HTML字符串

问题描述 投票:1回答:3

这很奇怪。我在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

javascript c# asp.net webmethod pagemethods
3个回答
0
投票

我在机器上创建了项目,工作代码如下:

<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错误。希望这可以帮助!


0
投票

我有同样的问题,这就是我解决它的方式。首先,从onclick标记中删除button属性:

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

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

然后用这行Javascript替换它:

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

按钮单击事件的默认事件处理程序将导致页面被提交,因此我们需要调用preventDefault [1] [2]来禁止此操作。


0
投票

对我来说,问题出在我的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);

...

答案是困惑的:

© www.soinside.com 2019 - 2024. All rights reserved.