这很奇怪。我的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函数
获得的响应答案 0 :(得分: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错误。希望这有帮助!
答案 1 :(得分: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]来禁止此操作。
答案 2 :(得分:0)
对我来说,问题出在我的App_Start/RouteConfig.cs
上。
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
也位于其中,则在WebMethod调用之前将PageMethods.set_path
添加到 JavaScript文件:...
PageMethods.set_path("YourPage.aspx");
PageMethods.SignupAccount(fname, lname, onSuccess, onFailure);
...
答案来自: