[英]Restify CORS not working for POST request
我正在為我的應用程序使用restify 。 跨源請求對於使用Restify的CORS的GET可以正常工作,但對於POST請求顯示以下錯誤。
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:1337/path/add. This can be fixed by moving the resource to the same domain or enabling CORS.
我使用的啟用CORS的代碼是:
server.use(restify.CORS());
根據其文檔 。 誰能建議我如何使POST請求生效
今天早上我遇到了同樣的問題,對我有用的更改是更改:
server.use(restify.CORS());
至:
server.pre(restify.CORS());
server.use(restify.fullResponse());
服務器。 前期是最重要的一點。 我在文檔中發現了與錯誤有關的錯誤:
在根文件中,無論您說什么,都說app.js或index.js或server.js
const cors = require('./cors');
const server = restify.createServer({
name : 'name',
version : 'version',
url : 'server url'
});
server.pre(cors.preflight);
server.use(cors.actual)
cors.js
const corsMiddleware = require('restify-cors-middleware');
const cors = corsMiddleware({
preflightMaxAge: 5,
origins: ['*'],
allowHeaders: ['*','token'],
exposeHeaders: ['*','token']
})
module.exports = cors
同樣的問題在這里解決了:
restify.CORS.ALLOW_HEADERS.push('authorization');
server.pre(restify.CORS());
server.use(restify.fullResponse());
假設您有一個授權標頭,例如承載令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.