您的位置:首页 > 理论基础 > 计算机网络

[置顶] Ajax获取数据时出现XMLHttpRequest cannot load

2017-04-18 11:06 495 查看
报错内容:

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

解决方案:

一、换火狐浏览器测试

二、将测试内容放到本地服务器,如:Tomcat上。

我的代码:(同时在同级目录下新建一个名为test.txt的文档,即可看到持续打印文档内的内容)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>autoUpdate</title>
<script>
var xmlhttp;
function populateList(){
var url = 'test.txt';
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange = processResponse;
xmlhttp.send(null);
}
function processResponse(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var li = document.createElement("li");
var txt = document.createTextNode(xmlhttp.responseText);
li.appendChild(txt);
document.getElementById("update").appendChild(li);
setTimeout(populateList,1000);
}else if(xmlhttp.readyState == 4 && xmlhttp.status != 200){
console.log(xmlhttp.responseText);
}
}
window.onload = function(){
xmlhttp = new XMLHttpRequest();
populateList();
}
</script>
</head>
<body>
<div id="update"></div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: