[置顶] 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>
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>
相关文章推荐
- ajax加载本地html文件出现 XMLHttpRequest cannot load的问题
- 用AJAX的时候出现XMLHttpRequest cannot load
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- ajax xmlhttprequest使用post传参数并向后台获取数据
- xmlhttprequest cannot load No 'Access-Control-Allow-Origin' AJAX跨域请求解决方法
- XMLHttpRequest cannot load file:///问题 Ajax本地跨域问题
- jQuery ajax XMLHttpRequest cannot load file
- 使用fetch/XMLHttpRequest/JQ Aajax获取数据
- 本地访问json格式文件出现XMLHttpRequest cannot load的解决方法
- 使用XMLHTTP Request Object获取服务器数据
- 又一个Ajax获取xmlHTTPRequest代码
- ajax中获取XMLHttpRequest对象的方法(IE/Firefox/chrome)
- 使用XMLHTTP Request Object获取服务器数据
- 使用XMLHTTP Request Object获取服务器数据 (转http://www.cnblogs.com/birdshome/archive/2004/12/26/82238.html)
- 1.使用XMLHttPRequest控件异步获取数据
- 使用XMLHTTP Request Object获取服务器数据
- Firefox3中使用XMLHttpRequest(Ajax)发送二进制POST数据的简易调试法
- 关于Microsoft.XMLHTTP或Ajax中异步获取数据的问题
- 原始ajax通过xmlHttpRequest对象的send()方法提交数据--Get方式和Post方式
- 1.使用XMLHttPRequest控件异步获取数据