gpt4 book ai didi

javascript - 因此,AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' 不允许访问

转载 作者:行者123 更新时间:2023-11-28 18:45:18 25 4
gpt4 key购买 nike

我正在尝试通过连接到 RESTful API 使用 AngularJS $http 服务进行 ajax 调用。

我得到的结果:

XMLHttpRequest cannot load http://hostname:8080/abc/test. No'Access-Control-Allow-Origin' header is present on the requestedresource. Origin 'null' is therefore not allowed access.

我知道我正在向不同的域执行XMLHttpRequest。因此浏览器会阻止它,因为出于安全原因它通常允许同一来源的请求。

请求的代码:

$http({
method: 'GET',
cache: true,
url: "http://hostname:8080/abc/test"
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
});

我在 stackoverflow 上读了很多问题:

No 'Access-Control-Allow-Origin' header is present on the requested resource error
Access-Control-Allow-Origin header
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access

但是没有成功。任何立即的帮助都将非常感激。谢谢

最佳答案

我得到了我发布的这个问题的答案,现在工作正常。

我们必须使用JSONP来防止跨源策略。

代码:

var url = "http://hostname:8080/abc/test?callback=JSON_CALLBACK";

$http.jsonp(url)
.success(function(data){
console.log(data);
});

关于javascript - 因此,AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' 不允许访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35431944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com