gpt4 book ai didi

angularjs - 跨源请求被阻止 : The Same Origin Policy disallows reading the remote resource at . ....

转载 作者:行者123 更新时间:2023-12-02 21:16:49 25 4
gpt4 key购买 nike

使用 $http.get 时遇到问题:跨源请求被阻止:同源策略不允许读取位于.....的远程资源,这可以通过将资源移动到同一域或启用 CORS 来解决

$http.get('...').success(function(data){
console.log(status);
}).error(function(data){
//console.log(data)
});
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}

最佳答案

跨源请求是从一个域向另一个域(在客户端)发出的请求。出于安全原因,默认情况下它们在 Web 中是被禁止的。因此,如果您有 http://www.yours.com 中的网页向 http://www.other.com/get/resource 发出 AJAX 请求,那么这个AJAX请求将会被拒绝。此问题可以通过 3 种方式解决:

  • 将 API (/get/resource)“重新定位”到 www.yours.com 域(即 http://www.yours.com/get/resource ),以便 AJAX 请求不再跨域。
  • 转换应用程序的架构,以便此请求来自 http://www.yours.com是在后端制作的(又名不是以 AJAX 的形式),然后传递到您的网页上。
  • http://www.other.com 中启用服务器允许来自 http://www.yours.com 的 CORS 请求。这可以通过在服务器的响应中添加 header Origin: http://www.yours.com 来完成。因此,它要求您有权访问第二台服务器的配置。

CORS 的其他资源:

  1. HTML5 turorial
  2. W3 specification

关于angularjs - 跨源请求被阻止 : The Same Origin Policy disallows reading the remote resource at . ....,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183775/

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