Restify CORS不适用于POST请求

时间:2014-07-15 10:54:34

标签: node.js cors restify

我正在为我的应用程序使用 restify 。交叉原始请求适用于 GET 并使用restify的CORS,但显示 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());

根据其documentation。任何人都可以建议我如何使 POST 请求工作

3 个答案:

答案 0 :(得分:2)

今天早上我遇到了同样的问题,对我有用的改变是改变:

server.use(restify.CORS());

要:

server.pre(restify.CORS());
server.use(restify.fullResponse());

服务器。是重要的一点。我发现了与文档中的错误相关的错误:

https://github.com/mcavage/node-restify/issues/573

答案 1 :(得分:1)

使用restify-cors-middleware   过程:

在根文件中,让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

答案 2 :(得分:0)

这里遇到同样的问题,用这个解决了:

top: some pixels ;
left: some pixels ;

假设您有一个授权标头,例如持票人令牌。