Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Node.js cross-origin POST. You should response for OPTIONS request first. Something like this.

View Node.js CORS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
if (req.method === 'OPTIONS') {
var headers = {};
// IE8 does not allow domains to be specified, just the *
// headers["Access-Control-Allow-Origin"] = req.headers.origin;
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
res.writeHead(200, headers);
} else {
//...other requests

Thanks! Pulled me out of a bind :)

C1D commented

Thanks a bunch for this!

Wow I like this code very much but was the else statement really necessary if you weren't going to put any code inside, just saying you could of saved a couple lines of code and made the program a bit shorter, Besides that I liked it good job.

Totally works. Thanks!


it actually works :)

Wow its moments like these that I remember that i 'know nothing!' Thanks!

Thank a lot! I've spent hours to make that work without extentions.

Qblack commented

Thank you, this works great but apparently if you trying to pass a token on the GET you may still need to add Authorization to the list of accepted Headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.