ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.43k stars 889 forks source link

cors error #423

Open woxiaobaimao opened 7 months ago

woxiaobaimao commented 7 months ago

When I initiate a post request, if there are no parameters, I can return success. If there are JSON parameters, I will encounter error 204 My Code:

include "crow.h"

include "crow/middlewares/cors.h"

int main() {

crow::App<crow::CORSHandler> app;
auto& cors = app.get_middleware<crow::CORSHandler>();
cors
    .global()
    .origin("*")
    .headers("origin, x-requested-with, accept, access-control-allow-origin, authorization, content-type")

    .methods("POST"_method, "GET"_method, "PUT"_method, "DELETE"_method, "PATCH"_method, "OPTIONS"_method)
    .allow_credentials();

// 处理 POST 请求的路由
CROW_ROUTE(app, "/sayHello")
    .methods(crow::HTTPMethod::Post)
    ([](const crow::request& req) {

        // 构造响应
        crow::json::wvalue response_json;
        response_json["message"] = "接收到的名字:" + 11;

        // 返回 JSON 数据
        return crow::response(response_json);
    });

// 启动服务器,监听端口 18080
app.port(18080).run();

}