Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Domain Ajax Call in Atom Shell

Tags:

cors

electron

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. Does Atom Shell (Electron) restrict cross domain calls by default?

like image 429
Raathigesh Avatar asked May 07 '15 12:05

Raathigesh


People also ask

Can you do cross domain ajax?

You can allow Cross Domain Ajax calls to an application by just registering a new filter and then configure it to Allow-Origin : {your domain's} or you can use a wild card “*” to allow the calls from all domains.

Which are the methods used for cross domain ajax calls?

For a successful cross-domain communication, we need to use dataType “jsonp” in jquery ajax call. JSONP or “JSON with padding” is a complement to the base JSON data format which provides a method to request data from a server in a different domain, something prohibited by typical web browsers.


3 Answers

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({
  webPreferences: { webSecurity: false }
});
like image 140
Lionep Avatar answered Oct 05 '22 13:10

Lionep


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.

"webPreferences" : {
    "webSecurity" : 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.

like image 39
ArkTekniK Avatar answered Oct 05 '22 14:10

ArkTekniK


Solutions' updated syntax:

var BrowserWindow = require('browser-window');
var win = new BrowserWindow({
    webPreferences: {webSecurity: false}
});
like image 28
mim Avatar answered Oct 05 '22 13:10

mim