This is weird. My onSuccess() function in the javascript file returns a massive HTML Code of my entire web page instead of the value I actually return from my C# WebMethod
1) I am running Visual Studio 2015 on Windows 10
2) When debugged, I kept a breakpoint at the SignupAccount C# method. The breakpoint was not hit. It didn't get to that point. BUT the onSuccess function was called
Here's an explanation:
ASP.NET Web Form's HTML:
<html xmlns="">
<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>
WebMethod in C# Code Behind:
[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() {}
}
Why is it happening? I believe the procedure and the code is correct. Is this something to do with Visual Studio?
EDIT
This is the response I get from the onSuccess function
This is weird. My onSuccess() function in the javascript file returns a massive HTML Code of my entire web page instead of the value I actually return from my C# WebMethod
1) I am running Visual Studio 2015 on Windows 10
2) When debugged, I kept a breakpoint at the SignupAccount C# method. The breakpoint was not hit. It didn't get to that point. BUT the onSuccess function was called
Here's an explanation:
ASP.NET Web Form's HTML:
<html xmlns="http://www.w3/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>
WebMethod in C# Code Behind:
[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() {}
}
Why is it happening? I believe the procedure and the code is correct. Is this something to do with Visual Studio?
EDIT
This is the response I get from the onSuccess function
http://pastebin./6MjAFPY9
Share Improve this question edited Jun 7, 2016 at 4:56 Jay asked Jun 7, 2016 at 4:39 JayJay 5,0849 gold badges78 silver badges142 bronze badges 11- Are you seeing any XHR entry to SignupAccount in browser Developer Tools -> Network Tab ? – Dhananjaya Kuppu Commented Jun 7, 2016 at 4:49
- @DhananjayaKuppu No I dont see any entry there. I tried creating a new web form with only a button and a web method and I still get this massive html string as the onSuccess return value. I think there's an issue with the config – Jay Commented Jun 7, 2016 at 4:51
- it could be duplicate to : post – Dhananjaya Kuppu Commented Jun 7, 2016 at 4:51
-
Perhaps the user isn't logged in, and they're being redirected to the login page..? Do you need to mark your
SignupAccount
as allowing anonymous users? – Rob ♦ Commented Jun 7, 2016 at 4:52 - @Rob Keeping the Signup example aside, I created a brand new web form with just a button and a WebMethod that returns "OK". I Still get this massive HTML String. I will edit the return value to the question now – Jay Commented Jun 7, 2016 at 4:53
3 Answers
Reset to default 6For me, the problem was in my App_Start/RouteConfig.cs
.
- Remove
settings.AutoRedirectMode = RedirectMode.Permanent;
- If
routes.EnableFriendlyUrls(settings);
is in there too, addPageMethods.set_path
to the JavaScript file, before your WebMethod call:
...
PageMethods.set_path("YourPage.aspx");
PageMethods.SignupAccount(fname, lname, onSuccess, onFailure);
...
The answer is puzzled together from:
PageMethod and URl Rewrite
ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
I created project on machine, working code is below:
<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>
Code behind:
[System.Web.Services.WebMethod]
public static string SignupAccount(string fname, string lname)
{
return "OK";
}
PS: Ensure you don't have any JavaScript errors on the page. Hope this helps!
I had the same problem and this is how I solved it. First, remove the onclick
attribute from the button
tag:
<!-- before -->
<button type="submit" id="submit-form" onclick="signup()">Sign In</button>
<!-- after -->
<button type="submit" id="submit-form">Sign In</button>
Then replace it with this line of Javascript:
$get('submit-form').addEventListener("click", function (event) {
signup();
event.preventDefault();
});
The default event handler for the button's click event will cause the page to be submitted, so we need to call preventDefault
[1] [2] to inhibit this.