Javascript Ajax错误:index.html:17未捕获的InvalidStateError:无法执行';发送';在';XMLHttpRequest';:对象';s状态必须打开。sendAjax
使用xampp在本地主机上运行index.html时,出现错误: index.html:17 Uncaught InvalidStateError:未能对“XMLHttpRequest”执行“发送”:对象的状态必须是打开的。sendAjax@index.html:17onclick@index.html:28 以下是index.html文件:Javascript Ajax错误:index.html:17未捕获的InvalidStateError:无法执行';发送';在';XMLHttpRequest';:对象';s状态必须打开。sendAjax,javascript,html,ajax,Javascript,Html,Ajax,使用xampp在本地主机上运行index.html时,出现错误: index.html:17 Uncaught InvalidStateError:未能对“XMLHttpRequest”执行“发送”:对象的状态必须是打开的。sendAjax@index.html:17onclick@index.html:28 以下是index.html文件: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Show Ajax</title>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4){
document.getElementById('ajax').innerHTML = xhr.responseText;
}
xhr.open('GET', 'data.html');
};
function sendAjax(){
xhr.send();
document.getElementById('load').style.display = "none";
}
</script>
</head>
<body>
<h1>Bring on ajax</h1>
<button id="load" onClick="sendAjax()">bring it</button>
<div id="ajax">
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<p> Hello I'm Ajax, came here on your request</p>
</body>
</html>
显示Ajax
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==4){
document.getElementById('ajax').innerHTML=xhr.responseText;
}
open('GET','data.html');
};
函数sendAjax(){
xhr.send();
document.getElementById('load').style.display=“无”;
}
带上ajax
拿来
下面是data.html文件:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Show Ajax</title>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4){
document.getElementById('ajax').innerHTML = xhr.responseText;
}
xhr.open('GET', 'data.html');
};
function sendAjax(){
xhr.send();
document.getElementById('load').style.display = "none";
}
</script>
</head>
<body>
<h1>Bring on ajax</h1>
<button id="load" onClick="sendAjax()">bring it</button>
<div id="ajax">
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<p> Hello I'm Ajax, came here on your request</p>
</body>
</html>
无标题文件
你好,我是阿贾克斯,是应你的要求来的
您不能在readyState
处理程序内打开请求,因为该处理程序仅在发出请求时才被激发,打开后,您必须在调用readyState
处理程序之前打开请求,但在定义后打开请求,这意味着在函数之外,在它之后
通常,您还应该为sendAjax
函数的每次调用创建一个新的请求对象,除非您有很好的理由不这样做
function sendAjax(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('ajax').innerHTML = xhr.responseText;
}
};
xhr.open('GET', 'data.html');
xhr.send();
document.getElementById('load').style.display = "none";
}
您不能在
readyState
处理程序内打开请求,因为该处理程序仅在发出请求时启动,打开后,您必须在调用readyState
处理程序之前打开请求,但在定义后打开请求,这意味着在函数之外,在它之后
通常,您还应该为sendAjax
函数的每次调用创建一个新的请求对象,除非您有很好的理由不这样做
function sendAjax(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('ajax').innerHTML = xhr.responseText;
}
};
xhr.open('GET', 'data.html');
xhr.send();
document.getElementById('load').style.display = "none";
}
谢谢你,伙计!您是对的,我在readystate中打开了请求,我只是将其从函数中删除,现在可以正常工作了。我很感激。:)谢谢你,伙计!您是对的,我在readystate中打开了请求,我只是将其从函数中删除,现在可以正常工作了。我很感激。:)