-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Closed
Description
server.cc:
void runServer(Database &database)
{
// Generate server object
httplib::Server svr;
//other svr.get, svr.post in between
svr.Put("/city", [&database](const httplib::Request &req, httplib::Response &res)
{
// Parse request body and define repsonse object
nlohmann::json requestBody = nlohmann::json::parse(req.body), response;
// Ensure all required fields are present
if (requireFields(res, requestBody, {"id", "field", "value"}, response))
{
// Extract fields
std::string id = requestBody["id"];
std::string field = requestBody["field"];
std::string value = requestBody["value"];
// Call helper function
// Fill in response object
database.UpdateCity(id, field, value);
response["response"]["success"] = true;
}
// Stringify response and set as response
res.set_header("Access-Control-Allow-Origin", "*");
res.set_content(response.dump(), "text/json");
database.PrintTables();
});
}
App.js:
const printDB = (event) => {
if (event.key === "Enter") {
fetch("http://localhost:8080/city", {
method: "PUT",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
id: "NOmDpk5F3Bp1ZkOvTgpe",
field: "CITY_NAME",
value: "nyc",
}),
});
}
};
Currently, the code in the server.cc works well using curl calls. However, when I have a client that sends a request to the server which updates the things in a database, there is a CORS issue that prevents the request from going through. I have tried using res.set_header("Access-Control-Allow-Origin", "*");
but the same error message keeps popping up. I'm not sure what I am missing at this point.
I have the res.set_header() line but the error message shows that it isn't detected. I've also searched in closed issues and didn't really get much help from that.
Metadata
Metadata
Assignees
Labels
No labels