我正在尝试使用 their api 从“darksky.net”获取信息.
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
请仅使用纯 JavaScript 回答。
最佳答案
来自darksky FAQ ,您应该为 API 调用设置代理服务器。
对纯 JavaScript 感到抱歉。这是不可能的。
Why do I get the error No 'Access-Control-Allow-Origin' header is present on the requested resource when I try to call the API?
We take security very seriously at Dark Sky. As a security precaution we have disabled cross-origin resource sharing (CORS) on our servers.
Your API call includes your secret API key as part of the request. If you were to make API calls from client-facing code, anyone could extract and use your API key, which would result in a bill that you'd have to pay. We disable CORS to help keep your API secret key a secret.
To prevent API key abuse, you should set up a proxy server to make calls to our API behind the scenes. Then you can provide forecasts to your clients without exposing your API key.
关于javascript - 从 darksky.net 请求数据时出现 Access-Control-Allow-Origin 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341661/