【发布时间】:2015-10-21 17:03:49
【问题描述】:
我的代码: var answer_array = [];
var req = new XMLHttpRequest();
req.onload = function() {
answer_array = answer_array.concat(JSON.parse(this.responseText).results);
console.log(answer_array);
}
req.open("GET", "https://api.comettracker.com/v1/gpsdata?fromdate=2015-10-13");
req.setRequestHeader("authorization", "Basic Base64 encoded credentials");
req.setRequestHeader("cache-control", "no-cache");
req.setRequestHeader("postman-token", "b94725ff-408b-c82e-a985-6c38feb380af");
req.send();
这是我控制台中的内容: scripts2.js:22 选项https://api.comettracker.com/v1/gpsdata?fromdate=2015-10-13(匿名函数)@scripts2.js:22 2015-10-21 12:41:09.059 index.html:1 XMLHttpRequest 无法加载 https://api.comettracker.com/v1/gpsdata?fromdate=2015-10-13。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问。响应的 HTTP 状态代码为 405。
当我转到 Chrome 上的网络标签时,我看到了这个: gpsdata?fromdate=2015-10-13 选项 405 xhr scripts2.js:22 0 B 452 ms
【问题讨论】:
-
好吧,这是一个 CORs 问题。
-
这是否意味着我无法从浏览器运行此脚本?
-
不,是的:这意味着它是服务器端的东西。如果您有权访问服务器端代码,则必须将“修复”放在那里
标签: javascript ajax http authentication basic-authentication