![](/img/trans.png)
[英]React JS - No 'Access-Control-Allow-Origin' header is present on the requested resource. Cross Origin Resource Error
[英]No Access-Control-Allow-Origin' header is present on the requested resource error in react-redux desktop application
我正在從 API 獲取數據,但沒有用。 它給了我這樣的錯誤" Access to fetch at 'http://muslimsalat.com/lahore/false/5.json?key=API_KEY' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. "
當我在 react-native mobile 中獲取數據時,它可以工作,但是當我在 react 桌面中獲取數據時,它就不起作用了。
誰能告訴我這個錯誤的原因是什么,我該如何解決?
將 CORS 添加到 API URL。 您只需將本地 IP 添加到后端服務(API)。 如果您需要適當的代碼來讓我們知道您用於后端 API 的語言。
CORS是一種機制,一個域可以在其中指定哪些其他域可以向它發出請求。 默認情況下,沒有Access-Control-Allow-Origin
標頭,這意味着例如stackoverflow.com
的腳本無法調用google.com
:
fetch('https://www.google.com') .then(console.log) .catch(console.error) // Error: Failed to fetch
這就是作為腳本運行的 React 應用程序無法獲取網絡上大部分內容的原因。
但是,React Native 應用程序作為應用程序運行,而不是作為腳本運行,因此它不受 CORS 的影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.