[英]Fetch API Error - preflight request doesn't pass access control check
我正在嘗試實現獲取API,以從我們的服務器獲取資源。 我可以使用Postman以及將端點粘貼到瀏覽器中來檢索資源。 但是,當我嘗試使用訪存API擊中端點時,出現以下錯誤:
從郵遞員撥打電話時,我得到以下結果:
從應用程序撥打電話時,我得到以下信息:
我不得不隱藏實際端點,因為它們是公司端點,因此我無法公開它們。 如果這引起了問題,請告訴我,也許我可以使用添加更多信息。
請求代碼:
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;
}
}
當您發出跨域請求時,通常會發生這種情況。 要解決此問題,您必須在服務器上啟用CORS(跨源資源共享) https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.