Reputation: 782
I'm using restify for my application. The cross origin requests works fine for GET with restify's CORS but shows following error for POST request.
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.
The code for enabling CORS i've used is:
server.use(restify.CORS());
as per its documentation. Can anyone suggest me how to make the POST request work
Upvotes: 1
Views: 2893
Reputation: 31
use restify-cors-middleware Procedure:
in root file let say app.js or index.js or server.js whatever you named
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
Upvotes: 1
Reputation: 2547
Same problem here, solved with this:
restify.CORS.ALLOW_HEADERS.push('authorization');
server.pre(restify.CORS());
server.use(restify.fullResponse());
That's assuming you have an authorisation header, for example for a bearer token.
Upvotes: 0
Reputation: 12317
I have run into the same problem this morning, the change that worked for me was to change:
server.use(restify.CORS());
To:
server.pre(restify.CORS());
server.use(restify.fullResponse());
the server.pre being the important bit. I found a bug relating to an error in the documentation:
https://github.com/mcavage/node-restify/issues/573
Upvotes: 2