javascript - 从 darksky.net 请求数据时出现 Access-Control-Allow-Origin 错误

标签 javascript

我正在尝试使用 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
                });
            }
        };

error in chrome

请仅使用纯 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/

相关文章:

javascript - 移动Webkit和Hammer JS上的多个元素响应触摸事件

javascript - 在另一个指令中使用 ui-tinymce 指令

javascript - 从 URL 上传图像到 Parse

javascript - LDAP over TLS 在 Node 中使用 ldap-client

javascript - Bootstrap 折叠侧边栏菜单到下拉菜单

javascript - 将 jquery 中的 css 添加到具有指定类名和文本的元素

javascript - 数组的产品,除了自己的 Javascript

javascript - 检测应用程序是否从页面加载

javascript - 触发使用 jquery 选择的文件数

javascript - 如何在 javascript 中使用 CryptoJS