Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

how to add header Access-Control-Allow-Origin ? #66

Open
blastbeng opened this issue Dec 16, 2019 · 2 comments
Open

how to add header Access-Control-Allow-Origin ? #66

blastbeng opened this issue Dec 16, 2019 · 2 comments

Comments

@blastbeng
Copy link

Hi,
I have the following error:
"...has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

so i need to add the Access-Control-Allow-Origin header, actually I am using the json_data example

How to do that?

Thanks and Regards

@kiyoMatsui
Copy link

Hi blastbeng... CORS should not be disabled when using curl. I guess you are calling from a browser? If you could provide more info so I can reproduce it I'll have another look. I think in the end you should do something like res.set_header("Access-Control-Allow-Origin", "http://someurl.*"); or something like that...

@shlomnissan
Copy link

shlomnissan commented Jun 21, 2020

@blastbeng when I was trying to access the API directly from a browser my primary issue with CORS was the preflight request. I ended up wrapping my handlers with the following for the time being

mux.use_wrapper([](served::response & res, const served::request & req, const std::function<void()> & handler){
    res.set_header("Access-Control-Allow-Origin", "*");
    if (req.method() == served::method::OPTIONS) {
        res.set_header("Access-Control-Allow-Headers", "*");
        res.set_status(200);
    } else {
        handler();
    }
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants