我刚刚开始使用ajax并遵循一些教程,我在尝试运行此html文件和链接的JavaScript文件时遇到此错误,
我知道浏览器无法发出文件请求//所以我启动了正在侦听端口8080并给出该文件地址的http-server。 这是我的javascript文件中的代码:
它只是一个简单的ajax请求和响应模型
print
HTML文件:
function test() {
if (window.XMLHttpRequest) {
//when request is made you wanna do it with XMLHttpRequest method!
httpRequest = new XMLHttpRequest();
console.log('xmlhhtprequest object was created')
} else if (window.ActiveXObject) {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
console.log('activex object was created')
}
//checking if request was successful
if (httpRequest.status === 200) {
console.log('request was successful')
} else {
console.log('request was unsuccesful')
}
//when response is recieved
httpRequest.onreadystatechange = function () {
//checking for response is recieved
if (httpRequest.readystate === XMLHttpRequest.DONE) {
document.getElementById('result').innerHTML
= httpRequest.responseText;
} else {
console.log('Error Occured')
}
}
//actually making request
httpRequest.open('GET', 'http://192.168.43.227:8080/Desktop/
simpleserver/test.txt', true);
httpRequest.send();
}
答案 0 :(得分:0)
由于其跨源访问错误,Chrome中提供的插件很少,无法提出跨域请求,最好是 Chrome CORS 插件。然后在命令行中使用以下命令禁用chrome中的跨域访问安全性。 在命令提示符下运行以下命令。
start chrome --args --disable-web-security --user-data-dir
确保在运行此命令之前关闭所有chrome实例。成功运行后,您将在Chrome浏览器下看到黄色条带。