我的javascript代码: -
function validate() {
debugger;
var username = document.getElementById('<%=txtUserName.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
if (username == "") {
alert("Please enter username.");
return false;
}
else if (password == "") {
alert("Please enter password.");
return false;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 1) {
}
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
console.log(xmlhttp.responseText);
if (xmlhttp.responseText == "True") {
window.location = "/home.aspx";
}
else if (xmlhttp.responseText == "Admin") {
window.location = "/Admin/adminhome.aspx";
}
else if (xmlhttp.responseText == "AlreadyLogin") {
var result = confirm("You are already logged in to the EMS. Do you want to forcefully close the session?");
if (result == true) {
window.location = "/loggedout.aspx";
return false;
}
else {
return false;
}
}
else if (xmlhttp.responseText == "InActive") {
var msg = "Your Profle is inactive in EMS. Please contact system administrator.";
alert(msg);
return false;
}
else if (xmlhttp.responseText == "NoRecord") {
var msg = "No record found for the entered details, Please enter proper details and try again.";
alert(msg);
return false;
}
else if (xmlhttp.responseText == "Incomplete") {
window.location = "/profile.aspx";
}
else if (xmlhttp.responseText == "Invalidemail") {
var msg = "Your email id is incorrect / not found in EMS. Please contact system administrator.";
alert(msg);
return false;
}
else {
var msg = "Login details are incorrect ! Please enter valid username & password.";
alert(msg);
return false;
}
}
}
if (window.location.protocol != "https:") {
xmlhttp.open("GET", "http://" + document.getElementById('hServerName').value + "/default.aspx?username=" + encodeURIComponent(username) + "&pwd=" + encodeURIComponent(password) + "&RID=" + Math.random(), false);
}
else {
xmlhttp.open("GET", "https://" + document.getElementById('hServerName').value + "/default.aspx?username=" + encodeURIComponent(username) + "&pwd=" + encodeURIComponent(password) + "&RID=" + Math.random(), true);
}
xmlhttp.send(null);
}
</script>
如果login为true,我的aspx.cs会返回此值 的Response.Write( “真”); 如果验证失败 回复于( “InvalidUser”);
但我得到的回应是
InvalidUser
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="default.aspx?username=sadAS&pwd=sfsdf&RID=0.701311394572258" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="BBFlkD5jZHakWD5rXjfHMoHWdKuqZAwj2AXV4/Qk/0LRr6x1y5yVdag9oXnzN7XOnBx6jQAI45EUwvTuYs3ka5eY25ChQxc2FJu0d3V4BLc=" />
</div>
<div>
</div>
</form>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"63ad0249321742999c96ca09ba258250"}
</script>
<script type="text/javascript" src="http://localhost:49619/adaac911432147a689adc118fade29e7/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
我想要的只是顶部的文字。 我做错了什么?
同样的错误在ie和chrome中都会弹出。
答案 0 :(得分:0)
当您使用HttpHandlers
或Web API获得最小响应时,您希望使用对ASPX页面的AJAX调用。
示例HttpHandlers
:
using System.Web;
public class HelloWorldHandler : IHttpHandler
{
public HelloWorldHandler()
{
}
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.ContentType = "text/plain";
if(/* Your test here*/) {
Response.Write("True");
}else{
Response.Write("InvalidUser");
}
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
}