【发布时间】:2018-02-26 20:56:52
【问题描述】:
我在http://www.test.com 中添加了一个带有以下脚本的 API:
<script src="http://apiendpoint.com/api/v1/api.js"></script>
<div id="api" data-apikey="LA59CJI9HZ-KIJK4I5-3CKJC"></div>
api.js
$(function () {
apikey = $('#api').data('apikey');
$("#api").load("http://apiendpoint.com?apikey=" + apikey);
})
当我加载页面时,出现以下错误:
XMLHttpRequest 无法加载 apiendpoint 网址。 重定向自 'apiendpoint URL' 到 'apiendpoint URL' 有 被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头 存在于请求的资源上。起源 'test URL' 因此不允许访问。
在 apiendpoint.com 的路径中,我在 .htaccess 中添加了以下代码:
Header set Access-Control-Allow-Origin "*"
但它不起作用。
【问题讨论】:
-
使用浏览器工具的网络选项卡检查是否存在标头
-
同时清除浏览器缓存。
-
从 chrome 商店下载 CORS 插件,打开后尝试发出请求。
-
@Ferrybig 响应标头不包含“Access-Control-Allow-Origin:*”。但是当我直接加载 URL 时,它包含 Access-Control-Allow-Origin:*
-
我相信sideshowbarker 's answer here 将拥有解决此问题所需的所有信息
标签: javascript cors