served icon indicating copy to clipboard operation
served copied to clipboard

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

Open blastbeng opened this issue 4 years ago • 2 comments

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

blastbeng avatar Dec 16 '19 20:12 blastbeng

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...

kiyoMatsui avatar May 13 '20 21:05 kiyoMatsui

@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();
    }
});

shlomnissan avatar Jun 21 '20 01:06 shlomnissan