Cross Domain Ajax Call in Atom Shell

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

We are working with Atom Shell (Currently known as electron) to wrap a web application as desktop app and having trouble making cross domain ajax calls due to CORS restriction.

We also tried nw.js (Formerly known as Node-Webkit) and we had no problem making cross domain ajax call with it. Is Atom Shell (Electron) restricts cross domain calls by default ?

回答1:

If the webpage is loaded in "file://" mode and not served by an http server, you can make ajax calls by default.

If you still have troubles with CORS restrictions, you can set this option to the browser-window object :

var BrowserWindow = require('browser-window'); var win = new BrowserWindow({   'web-preferences': {'web-security': false} });


回答2:

There are two problems here

CORS restrictions, which prevent the client from initiating a request, and the Access-Control-Allow-Origin header which is set by the server.

The first problem is solved as mentioned by setting the web-security options on the Browser-window object.

"web-preferences" : {     "web-security" : false },

The second issue whereby Electron actually sends 'file://' as the value of the Origin in the request does not have a solution as far as I can tell. Your options are to allow 'file://' or '*' in the Access-Control-Allow-Origin header (server side).

I have actually requested that setting the origin on requests be allowed but I suspect it will not get much traction.



回答3:

Solutions' updated syntax:

var BrowserWindow = require('browser-window'); var win = new BrowserWindow({     webPreferences: {webSecurity: false} });


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!