New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kept receiving a 404 while trying to access a basic crow server from a js fetch request #264
Comments
You need to add cross origin support. |
@Shravan40 Can you please share me an example of how to enable the cors in crow? Thanks! |
You can try adding |
@Shravan40 I am not sure where to add this line tho? I don't see variable res anywhere in either crow server or my js request |
@percyteng : Please go through this issue thread. |
@Shravan40 if you are saying adding the header to my js request, I have tried plenty of times. But my issue doesn't seem like it's related to cross-origin at all especially when I have this chrome extension to explicitly enable no cors on any request. |
@Shravan40 I think I am getting what you are saying Tried to do this |
You're getting that error because you're trying to access a non-const method in a const variable. const crow::response& res; // <--- const variable - and is an invalid reference
res.add_header("Content-Type", "text/plain"); // <--- add_header modifies res (non-const method) The correct way to access the CROW_ROUTE(app, "/")([](const crow::request&, crow::response& response)
{
response.add_header(...);
// write your data with this
response.write(...);
return response;
}); |
@pierobot Hi! I actually tried that before, but I was getting these errors |
Oh sorry, it's been a while. This should do the trick CROW_ROUTE(app, "/")([](const crow::request&, crow::response& response) -> void
{
response.add_header(...);
// write your data with this
response.write(...);
response.end();
}); I believe you can also do CROW_ROUTE(app, "/")([]()
{
crow::response response;
response.add_header(...);
// write your data with this
response.write(...);
return response;
}); |
@pierobot that worked! but my issue is still not resolved but I am still getting the 404 error. and if I disabled the chrome extension for cors. I am also getting the Access-Controll-Allow-Origin error: No 'Access-Control-Allow-Origin' header is present on the requested resource. This error should be resolved by adding the header to res in crow response though. |
Perhaps #207 (comment) will help you |
@pierobot @Shravan40 is it possibly related to OPTIONS? Looking at the log, instead of getting a GET request, seems like my crow is getting an OPTIONS request which triggers a 404. But I have indeed sent a GET request from my js fetch tho |
Yes that seems to be the case. |
@pierobot I think I found a solution! I added both request types to my handler Really appreciate the help! |
Here is my basic crow server
crow::SimpleApp app;
I was trying to send a standard fetch request from Javascript, however, I kept receiving a 404 request from it.
When I tried to access the crow server on both browser and postman, they work!
So I am not sure if there is something wrong with my js fetch or just crow itself. Here is my fetch
return fetch('http://localhost:4500', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => {
console.log(response)
if (response.status != 200){
console.log('errors')
return
}
else{
return response.json()
}
})
.then((responseJson)=>{
console.log(responseJson)
})
.catch((error) => {
console.error(error);
});
The text was updated successfully, but these errors were encountered: