如何解决为什么我的帖子请求没有 readyState=4 和 status=200?
我的问题是我有一个服务器端 C# 代码和一个带有 .js 文件的客户端 .html 网站。 .html 站点是一个登录页面,在点击登录按钮后,我从这里发送访问者提供的用户名和密码。我使用 xmlHttpRequest POST 发送用户名和密码,并使用 HttpListener 接收它。当我在服务器端使用一个简单的代码只是为了将一个简单的“真实”字符串发送回网站时,通信工作正常。但是当我使用某些函数来确定我需要发回什么字符串时,它就停留在 readyState=1 和 status=0 上。
这是我的服务器端代码:
MainSystem master = new MainSystem();
HttpListener listener = null;
try
{
listener = new HttpListener();
listener.Prefixes.Add("http://localhost:5050/Service1/");
listener.Start();
while (true)
{
Console.WriteLine("waiting...");
HttpListenerContext context = listener.GetContext();
String[] temporaryCommandIdAndDetails = GetPostedText(context.Request).Split(";");
Message msg = new Message(Convert.ToInt32(temporaryCommandIdAndDetails[0]),temporaryCommandIdAndDetails[1]);
string requestResult = master.getRequestResult("TestDataBase",msg);
//string requestResult = "1;true";
context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(requestResult);
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.AppendHeader("Access-Control-Allow-Origin","*");
using (Stream stream = context.Response.OutputStream)
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(requestResult);
Console.WriteLine("Message sent..." + requestResult);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
还有 getPostedText 方法:
private static string GetPostedText(HttpListenerRequest request)
{
string text = "";
using (StreamReader reader = new StreamReader(request.InputStream,request.ContentEncoding))
{
text = reader.ReadToEnd();
}
return text;
}
这是HTML代码:
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100 p-b-160 p-t-50">
<form class="login100-form validate-form">
<span class="login100-form-title p-b-43">
Account Login
</span>
<div class="wrap-input100 rs1 validate-input" data-validate = "Username is required">
<input class="input100" type="text" name="username" id="usern">
<span class="label-input100">Username</span>
</div>
<div class="wrap-input100 rs2 validate-input" data-validate="Password is required">
<input class="input100" type="password" name="pass" id="passw">
<span class="label-input100">Password</span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn" onclick="loginAuth()">
Sign in
</button>
</div>
</form>
</div>
</div>
</div>
这是我的 .js 文件:
function loginAuth()
{
var xhttp = new XMLHttpRequest();
var username = document.getElementById("usern").value;
var password = document.getElementById("passw").value;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText.split(";");
var resultId = response[0];
var resultDetails = response[1];
if(resultDetails === "true")
{
location.replace("document_handler_home_page.html");
}
else
{
alert("Incorrect username or password!");
}
}
else {
alert(xhttp.readyState + "...." + xhttp.status);
}
};
xhttp.open("POST","http://localhost:5050/Service1",true);
xhttp.send("1;" + username + "$" + password);
};
当我在 c# 代码中注释掉以 String[] 临时命令...结束的三行字符串 requestResult = master... 并取消注释 //string requestResult ="1;true" 部分然后它工作并重定向我到主页。
有趣的是,当我不提供用户名或密码时,或者两者都不提供时,它也可以工作。它让我发疯......
提前谢谢各位。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。