我是Node.JS的新手,我正在尝试启动并运行我的客户端Javascript与我的Node.JS后端进行通信的项目。我一直在客户端使用XMLHTTPRequest()
,但返回Failed to load resource: Preflight response is not successful
。从我的理解我得到这个,因为浏览器不喜欢从Javascript发出HTTP请求?我究竟做错了什么?代码:
window.onload = function(){
httpGet("http://localhost:8080");
console.log("Loaded");
}
function httpGet(url){
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
console.log(this.status)
if (this.readyState == 4 && this.status == 200) {
console.log("Got names")
getNamesFromJSONArray(request.responseText);
};
};
request.open("GET", url, true);
request.setRequestHeader("Access-Control-Allow-Origin", "*")
request.send();
}
答案 0 :(得分:0)
paste0