我為來自 JSON 檔案的靜態 API 托管了一個站點。偽造的 API 鏈接是api.example.com/db.json.
當我使用郵遞員檢查 URL 時,我收到 200 個狀態碼。但是當我從 Javascript 中獲取它時,它顯示了這個錯誤Access to fetch at 'http://api.example.com/db.json' from origin 'null' has been blocked by CORS policy: 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.
我正在使用 nginx 運行 Web 服務器,它是一個簡單的站點包,只有一個 HTML 頁面和那個 db.json 檔案。我應該在哪里添加Access-Control-Allow-Origin: https://www.example.com?
另外,我試過 json-server
我什至試過json-server。它在本地運行,但是當我將它托管在 nginx 服務器中并嘗試運行時pm2 start --name=static_api npm run json:server
pm2 顯示此錯誤
[PM2] Starting /usr/local/bin/npm in fork_mode (1 instance)
[PM2] Done.
[PM2][ERROR] Script not found: /var/www/html/static_api/run
有什么辦法可以json-server用 Nginx 運行假 API 嗎?
謝謝。
uj5u.com熱心網友回復:
添加 add_header Access-Control-Allow-Origin "https://www.example.com"; 到您的 nginx 服務器塊(如果需要,以相同的方式和其他 CORS 標頭)。
伊萬·沙茨基的回答
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487231.html
上一篇:URL中帶有資料的Angular應用程式從NGINX獲取404
下一篇:使用Nginx進行身份驗證的方法
