gpt4 book ai didi

javascript - 响应文本比较

转载 作者:行者123 更新时间:2023-11-30 10:48:05 25 4
gpt4 key购买 nike

我正在练习 AJAX,因为我编写了一个代码来从服务器中的文件中获取文本,如果它是“0”则打印“零”或者如果错误打印“未连接”则打印“一”。但是出了点问题不知道是什么,即使连接了也没有连接..

代码如下:

<html>
<head>
<title>LogIN</title>
<script>
function verify()
{
var xml;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xml=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xml=new ActiveXObject("Microsoft.XMLHTTP");
}
xml.onreadystatechange=function()
{
if (xml.readyState==4 && xml.status==200)
{
var res=xml.responseText();
if(res.equals("0"))
{
document.write("zero");
}
else
{
document.write("one");
}
}
else
document.write("Not connected");
}
xml.open("GET", "log_verify.txt", true);
xml.send();
}
function login()
{
//action to login
}
</script>
</head>
<body>
<form>
User name : <input type="text" name="uname" onblur="verify()">
<br>
Pwd : <input type="password" name="passwd" >
<br>
<input type="button" name="Login" value="Login" onclick="login()">
</form>
</body>
</html>

获取输出为

Not connectedNot connectedNot connected

但是当我只显示响应文本时,它会按照下面的代码正确打印

<html>
<head>
<title>LogIN</title>
<script>
function verify()
{
var xml;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xml=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xml=new ActiveXObject("Microsoft.XMLHTTP");
}
xml.onreadystatechange=function()
{
if (xml.readyState==4 && xml.status==200)
{
document.getElementById("myDiv").innerHTML+=xml.responseText;
}
}
xml.open("GET", "log_verify.txt", true);
xml.send();
}
function login()
{
//action to login
}
</script>
</head>
<body>
<form>
User name : <input type="text" name="uname" onblur="verify()">
<br>
Pwd : <input type="password" name="passwd" >
<br>
<input type="button" name="Login" value="Login" onclick="login()">
</form>
<div id="myDiv"><h2>Response text:</h2></div>
</body>
</html>

获取输出为

Response text:

0

是 javascript 编码中的问题还是服务器响应中的某个地方??

最佳答案

问题 #1

在第一个片段中,您编写了 xml.responseText(),这会导致脚本终止。

在第二个片段中你做对了,写了 xml.responseText。它是一个文本属性,而不是一个函数。


问题#2

关于“未连接”的消息,没有问题。

您假设,当 onreadystatechange 被触发时,(xml.readyState==4 && xml.status==200) 如果已连接,如果未连接则相反.

但这不是真的。

在 XMLHttpRequest 的生命周期中,如果连接成功,onreadystatechange 会触发多次,随着请求的进行跟踪对象的各种状态。

这些状态(将它们的值借给 .readyState)列在 section 3.5 of the relevant W3C spec 下:

  • UNSENT (numeric value 0)

    The object has been constructed.

  • OPENED (numeric value 1)

    The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.

  • HEADERS_RECEIVED (numeric value 2)

    All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.

  • LOADING (numeric value 3)

    The response entity body is being received.

  • DONE (numeric value 4)

    The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

当对象进入DONE 状态时,您使用条件来执行您的代码,而不是因为任何其他状态指示失败。

The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

如果要查找此故障,请检查 .state 属性,which has the following possible values :

  • If the state is UNSENT or OPENED return 0 and terminate these steps.
  • If the error flag is true return 0 and terminate these steps.
  • Return the HTTP status code.

所以:

xml.onreadystatechange = function() {
if (xml.readyState != 4) { // handle DONE only
return;
}

if (xml.status == 0) { // error
document.getElementById("myDiv").innerHTML += "Connection error"
}
else if (xml.status == 200) { // HTTP 200 OK
document.getElementById("myDiv").innerHTML += xml.responseText;
}
else { // some other HTTP code
document.getElementById("myDiv").innerHTML += "HTTP response code " + xml.status;
}
}

关于javascript - 响应文本比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com