angularjs1.4 CORS 失败
angularjs1.4 CORS failed
我正在使用 AngularJS 1.4.3。这是我的代码:
angular
.module('app', [])
.run(run);
function run($http) {
a = $http({
method: "GET",
url: 'http://127.0.0.1:8080/test',
data: {}
});
console.log(a);
}
使用浏览器或Postman可以正确获取。但是上面的代码给出了
XMLHttpRequest cannot load http://127.0.0.1:8080/test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
关键是,我可以使用 GET 方法来使用应用程序获取结果。但是使用代码是行不通的。我做错了什么?
The point is, I can use GET method to get the result using applications.
在那种情况下,只有应用程序和服务器,没有其他人。只涉及两个实体。
But using code won't work.
您有两个不同的网站(localhost:8000 和 localhost:8080),浏览器不知道 8000 提供的 JavaScript 是否可以信任 8080 愿意提供的数据给用户。
What have I done wrong?
您没有在来自 8080 的 HTTP 响应中提供 Access-Control-Allow-Origin
header 来告诉浏览器 8000 可以信任数据。
另请参阅:
我正在使用 AngularJS 1.4.3。这是我的代码:
angular
.module('app', [])
.run(run);
function run($http) {
a = $http({
method: "GET",
url: 'http://127.0.0.1:8080/test',
data: {}
});
console.log(a);
}
使用浏览器或Postman可以正确获取。但是上面的代码给出了
XMLHttpRequest cannot load http://127.0.0.1:8080/test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
关键是,我可以使用 GET 方法来使用应用程序获取结果。但是使用代码是行不通的。我做错了什么?
The point is, I can use GET method to get the result using applications.
在那种情况下,只有应用程序和服务器,没有其他人。只涉及两个实体。
But using code won't work.
您有两个不同的网站(localhost:8000 和 localhost:8080),浏览器不知道 8000 提供的 JavaScript 是否可以信任 8080 愿意提供的数据给用户。
What have I done wrong?
您没有在来自 8080 的 HTTP 响应中提供 Access-Control-Allow-Origin
header 来告诉浏览器 8000 可以信任数据。
另请参阅: