其他分享
首页 > 其他分享> > 关于httplib CORS

关于httplib CORS

作者:互联网

关于httplib  CORS 

https://github.com/yhirose/cpp-httplib/pull/62

按照此链接需添加

svr.Options(R"(\*)", [](const auto& req, auto& res) {
    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
});

svr.Options("/resource/foo", [](const auto& req, auto& res) {
    res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin").c_str());
    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
    res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
    res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
});
其中/resource/foo改成你需要的,改完后大概会提示Access to XMLHttpRequest at 'http://127.0.0.1:10000/XXXXXX' from origin 'http://127.0.0.1' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

需添加

svr.Get("/XXXXXX", [this](const httplib::Request& req, httplib::Response& res)

{
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
.................................
});


标签:Control,httplib,set,res,Access,header,关于,Allow,CORS
来源: https://www.cnblogs.com/zlingshi/p/15457931.html