Enable CORS?
Closed this issue Β· 10 comments
I am using the javascritp fetch
api to call your basic server test code. Works perfectly with the vanilla GET
. I am having the trouble when I move to a more complicated POST
setup that is seems similar to this discussion. Do you have advice on how to resolve this? Thank you.
My error:
Access to fetch at 'http://35.238.222.54/bin' from origin 'http://localhost:3000' has been blocked by CORS
policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. If an opaque response serves your needs, set the request's
mode to 'no-cors' to fetch the resource with CORS disabled.
My code:
callpost(){
postData('http://xx.xxx.xxx.xx/bin', {"n": 4, "k": 3})
.then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
.catch(error => console.error(error));
}
export function postData(url = ``, data = {}) {
// Default options are marked with *
return fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
//mode: "no-cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses response to JSON
}
I think you might need to add an endpoint which accepts OPTIONS requests -- before sending POST requests browsers make pre-flight OPTIONS requests to see if it's OK to make POST requests and maybe that's also happening automatically here? There may also be a header you need to set on the client side -- I'll check one of my projects where I've done this on Monday.
Hi @amellnik . Have you had time to look at this yet? This is my first exposure to these ideas and I appreciate your experience.
When you state above, I think you might need to add an endpoint which accepts OPTIONS requests
, do you mean in Kubernetes or in the Julia Docker code?
This discussion encountered the CORS problem using Kubernetes by configuring their nginx server. Is there a similar approach we can take with our Julia server?
Here, the author suggests a general solution by rerouting through Heroku. That seems a little hacky, but I don't know. What do you think?
On the server I am using the default middleware item Joseki.add_cors!
and I have an endpoint that looks like (req -> req.response, "OPTIONS", "*")
. I don't think you should need to do anything beyond this. On the client the only header that I set is Content-Type
.
So, if I understand correctly, the only change I need to make is to change the endpoints like below? Because the Joseki.jl server I am using has the middleware item Joseki.add_cors!
already set up by default and my above header is already ok.
I donβt think I asked this correctly- do I have to change anything to enable Joseki.add_cors!
endpoints = [
(pow, "GET", "/pow"),
(bin, "POST", "/bin"),
(req -> req.response, "OPTIONS", "*")
]
That should work as written. Under the hood Joseki uses JSON.json to serialize objects and it should work happily on matrices, (which are represented as arrays of arrays in json).