繁体   English   中英

Ajax请求将整个页面发回,而不是我在Response.Write中发送的文本

[英]Ajax requests sends the whole page back instead of text i send in Response.Write

我的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>

如果登录为true,我的aspx.cs将返回此值。Response.Write(“ True”); 如果未能验证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&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>

我只想要最上面的文字。 我究竟做错了什么 ?

并且在ie和chrome中弹出相同的错误。

当您应该使用HttpHandlersWeb 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; }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM