example-crud icon indicating copy to clipboard operation
example-crud copied to clipboard

TypeError: Failed to fetch in Swagger

Open theTJCloudWalker opened this issue 2 years ago • 2 comments

I ran this project successfully but met TypeError when I test api, I am using a macbook air m1, no code is modified, so I was confused

theTJCloudWalker avatar Jan 27 '23 15:01 theTJCloudWalker

plus, response is fine when I use curl in cmd

theTJCloudWalker avatar Jan 27 '23 15:01 theTJCloudWalker

I found a way to sovle this problem https://github.com/oatpp/oatpp/blob/master/changelog/1.2.5.md#enable-global-cors enable cors for this example

OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {

    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
    OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper); // get ObjectMapper component

    auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
    connectionHandler->setErrorHandler(std::make_shared<ErrorHandler>(objectMapper));

    /* Add CORS-enabling interceptors */
    connectionHandler->addRequestInterceptor(std::make_shared<oatpp::web::server::interceptor::AllowOptionsGlobal>());
    connectionHandler->addResponseInterceptor(std::make_shared<oatpp::web::server::interceptor::AllowCorsGlobal>());
    
    return connectionHandler;

  }());

joygo avatar Dec 11 '23 02:12 joygo