Ajax获取数据时出现XMLHttpRequest cannot load

报错内容:

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>




猜你喜欢

转载自blog.csdn.net/liyuxing6639801/article/details/70226010