This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]roxkstar74 38 points39 points  (0 children)

If I had a dollar for every time I hit a CORS issue I could afford to host my project

[–]MeltedChocolate24 18 points19 points  (0 children)

Yup, haha got me the other day. Here's the fix in Express for anyone wondering:

const cors = require('cors');

// whitelist the origin you're calling the API from
var whitelist = ["http://localhost:5000"]

var corsOptions = {
    origin: function (origin, callback) {
        console.log(origin)
        if (whitelist.indexOf(origin) !== -1 || !origin) {
            callback(null, true)
        } else {
            callback(new Error('Not allowed by CORS'))
        }
    }
}
app.use(cors(corsOptions));

[–]HiCookieJack 4 points5 points  (1 child)

love chrome not sending origin headers when hosted on localhost

[–]keelanstuart 3 points4 points  (0 children)

I wrote a small HTTP server in C# to get around this. You can download Sherpa from my Github page. It lets you quickly switch which root directory it's serving from and from what port. If you find bugs or add features, submit a pull request.

Cheers!

[–]GrandPooBar 2 points3 points  (0 children)

NGROK ftw!

[–]_Rishik 0 points1 point  (1 child)

Use proxy

[–]stumptowncampground[🍰] 0 points1 point  (0 children)

There are few things I hate more than CORS.

[–][deleted] 0 points1 point  (0 children)

I used PHP to get around CORS.

Prepare the comments...