Javascript Ajax请求将整个页面发送回,而不是我发送的文本作为响应

Javascript Ajax请求将整个页面发送回,而不是我发送的文本作为响应,javascript,jquery,html,asp.net,ajax,Javascript,Jquery,Html,Asp.net,Ajax,我的javascript代码:- function validate() { debugger; var username = document.getElementById('<%=txtUserName.ClientID %>').value; var password = document.getElementById('<%=txtPassword.ClientID %>').value; if

我的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>
函数验证(){
调试器;
var username=document.getElementById(“”).value;
var password=document.getElementById(“”).value;
如果(用户名==“”){
警报(“请输入用户名”);
返回false;
}
else if(密码==“”){
警报(“请输入密码”);
返回false;
}
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==1){
}
if(xmlhttp.readyState==4&&xmlhttp.status==200){
警报(xmlhttp.responseText);
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(“您已经登录到EMS。是否要强制关闭会话?”);
如果(结果==真){
window.location=“/loggedout.aspx”;
返回false;
}
否则{
返回false;
}
}
else if(xmlhttp.responseText==“非活动”){
var msg=“您的产品在EMS中处于非活动状态。请与系统管理员联系。”;
警报(msg);
返回false;
}
else if(xmlhttp.responseText==“NoRecord”){
var msg=“未找到输入详细信息的记录,请输入正确的详细信息,然后重试。”;
警报(msg);
返回false;
}
else if(xmlhttp.responseText==“未完成”){
window.location=“/profile.aspx”;
}
else if(xmlhttp.responseText==“Invalidemail”){
var msg=“您的电子邮件id不正确/未在EMS中找到。请与系统管理员联系。”;
警报(msg);
返回false;
}
否则{
var msg=“登录详细信息不正确!请输入有效的用户名和密码。”;
警报(msg);
返回false;
}
}
}
如果(window.location.protocol!=“https:”){
xmlhttp.open(“GET”、“http://”+document.getElementById('hServerName')。value+”/default.aspx?username=“+encodeURIComponent(username)+”&pwd=“+encodeURIComponent(password)+”&RID=“+Math.random(),false);
}
否则{
xmlhttp.open(“GET”、“https://”+document.getElementById('hServerName')。value+“/default.aspx?username=“+encodeURIComponent(username)+”&pwd=“+encodeURIComponent(password)+”&RID=“+Math.random(),true);
}
xmlhttp.send(空);
}
如果登录为真,我的aspx.cs将返回此信息 回答:填写(“真实”); 如果验证失败 响应。写入(“无效用户”)

但我得到的回应是

    InvalidUser

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="default.aspx?username=sadAS&amp;pwd=sfsdf&amp;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>
InvalidUser
{“appName”:“Chrome”,“requestId”:“63ad0249321742999c96ca09ba258250”}
我想要的只是上面的文字。 我做错了什么


ie和chrome中都会出现同样的错误。

您希望在应该使用的时候使用对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; }
    }
}

您希望在应该使用的时候使用对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; }
    }
}

您不应该使用
返回“yourtext”而不是
响应
我试图返回,但是它没有任何值,只有html内容,
.cs
页面中的方法被命中了吗?你在VisualStudio中调试过吗?是的,它得到了。我得到的值是一个html页面,而不是我返回的文本。您是否应该使用
返回“yourtext”
而不是
响应
我试图返回,但是它没有任何值,只有html内容,
.cs
页面中的方法被命中了吗?你在VisualStudio中调试过吗?是的,它得到了。我得到的值是一个html页面,而不是我返回的文本