gpt4 book ai didi

Ajax 函数调用

转载 作者:行者123 更新时间:2023-12-01 05:42:38 25 4
gpt4 key购买 nike

我编写了如下的 Ajax 函数。

它工作不正常。如果我删除 xmlhttp.status==400然后它正在工作。我在这个例子中犯了什么错误?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function getAjax()
{
if (window.XMLHTTPRequest)
{
xmlhttp=new XMLHTTPRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.xmlHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==400)
{
document.getElementById('mydiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","testajax.txt",true);
xmlhttp.send(null);
}
</script>
</head>

<body>
<input type="button" value="Get content" onclick="getAjax()"><br>
<div id="mydiv"></div>
</body>
</html>

最佳答案

"Another simple use is finding if a url exists, in HTTP there are various status codes returned by both HEAD and GET requests, 200 means success, 404 means failure, and the others mean other things. See HTTP status codes for a full explanation."



使用 xmlhttp 对象的 status 属性为您提供此状态
         if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) alert("URL Exists!")
else if (xmlhttp.status==404) alert("URL doesn't exist!")

}

http://www.jibbering.com/2002/4/httprequest.2004.9.html

关于Ajax 函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4446974/

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