has been blocked by CORS policy: Response to preflight request doesn‘t pass access control 跨域解决

 

情况一,普通请求

在响应头中添加指定头信息

 

http_response sp(status_codes::OK);
sp.headers().add("Access-Control-Allow-Origin","*");
sp.headers().add("Access-Control-Allow-Methods","*");
sp.headers().add("Access-Control-Allow-Headers","*");
sp.set_body(msg.serialize(),"application/json");
resp.reply(sp);

`

情况二,复杂请求

比如上传文件,浏览器会先发一个OPTIONS请求作为preflight。这时除了上面在正常处理的如POST响应里加指定头部。还需要单独处理OPTIONS的响应,并添加头部。

 

m_listener->support(methods::OPTIONS,std::bind(&MSHttpServer::handle_options, this, std::placeholders::_1));

`

你可能感兴趣的:(其他,http,跨域,C++,CORS,preflight)