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
No 'Access-Control-Allow-Origin' error on for origin 'file://' #1990
Comments
You can use |
@zcbenz this is my fetch code fetch(api + 'oauth/token', {
mode: 'no-cors',
method: 'post',
body: form
}).then((response) => response.json()).then(function(data) {
console.log(data.access_token);
}); after use 'no-cors' mode, it will cause an error in response.json():
although I can see the json response in Devtools Network Panel, response.text() will be empty in this case. |
This error means |
@zcbenz yes, but it is a valid json instead of empty string when I ran this app without Since I set
in main.js, I expect that fetch should work with |
@kidwm The response headers have to explicit set the file's type to JSON to allow the |
@zcbenz There is no content in Response with Now I turn into using superagent instead of native window.fetch. |
@kidwm Can you share your superagent code for it? |
As long as I've tried, the config should be:
And then you don't have to use It works as if a native app. |
The problem here, is that, in cases like OAuth2, you don't have rights to add And even with This is really counterintuitive as electron is a native application, but it's right now difficult to make no-cors requests. |
hbrls's answer works. |
Worked for me using electron-vue. Had to pass the web-preferences into the createWindow options. |
Just overide header before send request like this
put these codes in renderer process |
@tuan072090 Thanks Hero! |
Is the working solution |
What is the recommended way to make a web request?
When I try to make a web request using
npm fetch
I get the following errorFetch API cannot load https://foobar.com/YadaYada. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access.
I have also set the following as my browser-window options, but it has not resolved the issue.
Does this mean I can not make API requests to different servers? Since all of my javascript code is going to be run on a local file:// path ?
The text was updated successfully, but these errors were encountered: