Skip to content
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

Closed
mattotodd opened this issue Jun 16, 2015 · 14 comments
Closed

No 'Access-Control-Allow-Origin' error on for origin 'file://' #1990

mattotodd opened this issue Jun 16, 2015 · 14 comments

Comments

@mattotodd
Copy link

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 error

Fetch 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.

'web-preferences': {
  'web-security': false
}

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 ?

@max-mapper max-mapper changed the title Recommended way to make web requests ? No 'Access-Control-Allow-Origin' error on for origin 'file://' Jun 16, 2015
@zcbenz
Copy link
Member

zcbenz commented Jun 17, 2015

You can use fetch with “no-cors” mode, see also:
https://developers.google.com/web/updates/2015/03/introduction-to-fetch?hl=en

@zcbenz zcbenz closed this as completed Jun 17, 2015
@kidwm
Copy link

kidwm commented Jul 8, 2015

@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():

Uncaught (in promise) SyntaxError: Unexpected end of input

although I can see the json response in Devtools Network Panel, response.text() will be empty in this case.

@zcbenz
Copy link
Member

zcbenz commented Jul 9, 2015

This error means response is not a valid JSON string.

@kidwm
Copy link

kidwm commented Jul 9, 2015

@zcbenz yes, but it is a valid json instead of empty string when I ran this app without no-cors in localhost:3000 which is allowed in 'Access-Control-Allow-Origin' header of my api server.

Since I set

'web-preferences': {
        'web-security': false
    }

in main.js, I expect that fetch should work with file:// protocol, or did I get something wrong?

@zcbenz
Copy link
Member

zcbenz commented Jul 10, 2015

@kidwm The response headers have to explicit set the file's type to JSON to allow the .json(), and apparently the url with file:// doesn't have any header so the call is not allowed. You have to manually call JSON.parse, or you can create an issue in Chromium project on this.

@kidwm
Copy link

kidwm commented Jul 13, 2015

@zcbenz There is no content in Response with no-cors. So JSON.parse will fail.
I just wonder why 'web-security': false does not work with window.fetch.

Now I turn into using superagent instead of native window.fetch.

@hbrls
Copy link

hbrls commented Oct 4, 2016

@kidwm Can you share your superagent code for it?

@hbrls
Copy link

hbrls commented Oct 4, 2016

As long as I've tried, the config should be:

webPreferences: {
    webSecurity: false
}

And then you don't have to use fetch (url, { mode: 'no-cors' }).

It works as if a native app.

@NdYAG
Copy link

NdYAG commented Oct 8, 2016

The problem here, is that, in cases like OAuth2, you don't have rights to add Access-Control-Allow-Origin header onto the response.

And even with loadURL('file://${__dirname}/index.html'), {webSecurity: false} and fetch(url, {mode: 'no-cors'}), the response is still opaque response in renderer process and could not be accessed (while the request display the full valid json response in devtools).

This is really counterintuitive as electron is a native application, but it's right now difficult to make no-cors requests.

@erguotou520
Copy link

erguotou520 commented Jan 17, 2018

hbrls's answer works.

@tetsuo-matsumura
Copy link

@zcbenz yes, but it is a valid json instead of empty string when I ran this app without no-cors in localhost:3000 which is allowed in 'Access-Control-Allow-Origin' header of my api server.

Since I set

'web-preferences': {
        'web-security': false
    }

in main.js, I expect that fetch should work with file:// protocol, or did I get something wrong?

Worked for me using electron-vue. Had to pass the web-preferences into the createWindow options.

@tuan072090
Copy link

Just overide header before send request like this

const filter = { urls: ['*://*.google.com/*'] }; const session = electron.remote.session session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => { details.requestHeaders['Origin'] = null; details.headers['Origin'] = null; callback({ requestHeaders: details.requestHeaders }) });

put these codes in renderer process

@camdagr8
Copy link

@tuan072090 Thanks Hero!

@Nashorn
Copy link

Nashorn commented Feb 14, 2020

Just overide header before send request like this

const filter = { urls: ['*://*.google.com/*'] }; const session = electron.remote.session session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => { details.requestHeaders['Origin'] = null; details.headers['Origin'] = null; callback({ requestHeaders: details.requestHeaders }) });

put these codes in renderer process
const filter = { urls: ['://.google.com/*'] }; const session = electron.remote.session session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => { details.requestHeaders['Origin'] = null; details.headers['Origin'] = null; callback({ requestHeaders: details.requestHeaders }) });

Is the working solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants