获取API错误 - 预检请求未通过访问控制检查

时间:2018-04-22 15:58:28

标签: javascript rest fetch-api

我正在尝试实现fetch api以从我们的服务器获取资源。我可以使用Postman检索资源,也可以将端点粘贴到浏览器中。但是,当我尝试使用fetch api命中端点时,我收到以下错误:

enter image description here

从Postman拨打电话时,我得到以下结果:

enter image description here

从应用程序拨打电话时,我得到以下信息:

enter image description here

我不得不隐藏到实际端点,因为它们是公司端点,我无法公开它们。如果这导致问题,请告诉我,也许我可以使用添加更多信息。

请求代码:

return fetch(url, {
    method: 'get',
    headers: this._headers()
})
.then(data => {
    console.log(data);
    return data;
})
.then(this._checkStatus)
.then(res => res.json());


_headers(auth) {
    let headers = new Headers({
        'Content-Type': 'application/json'
    });

    return headers;
}

_checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
        return response;
    } else {
        let error = new Error(response.statusText);
        error.response = response;
        throw error;
    }
}

1 个答案:

答案 0 :(得分:0)

通常在您进行跨域请求时发生。 要解决此问题,您必须在服务器https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

上启用CORS(跨源资源共享)
相关问题