使用电子访问跨源资源

Using electron to access cross-origin-resources

我在跟踪任务的共享点上有一个列表。

我正在尝试创建一个 electron 应用程序,它将每分钟左右 ping(http get 请求)此列表并显示一点 window 当前用户分配的所有任务并突出显示新任务。

我正在使用 fetch API 访问列表,如下所示:

const _COLLAB_ROOT = "http://company.com/projects/team-site/_vti_bin/listdata.svc/"

export function read(list, callback) {
    const myHeaders = new Headers({
        "Accept": "application/json",
        'Authorization': 'Basic '+btoa('username:password'),
        'Access-Control-Allow-Origin': '*'
    });
    const myInit = {
        method: 'GET',
        headers: myHeaders,
        mode: 'no-cors'
    }
    fetch(_COLLAB_ROOT+list,myInit)
        .then(response => {
            if (response.ok) {
                response.json().then(data => {
                    callback(data.d);
                });
            }
            return Promise.reject(Error('error'))
        }).catch(error => {
            return Promise.reject(Error(error.message))
        })
}

其他模块:

read('listname',data => {
  console.log(data);
})

然而,当我发送这个填写了列表名称的请求时,我得到以下信息:

现在我假设这与 CORS 有关。我想知道的是,有没有办法让它工作?

在electron中似乎需要一些非常明显的东西。

此外,我已将我们的首选项 属性 设置为

mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    frame: true,
    'web-preferences': {'web-security': false}
  });

在此感谢任何帮助。 如果这是不可能的,我会感到非常惊讶,所以希望我是愚蠢的!

编辑:在 chrome

从 Restlet 客户端查询时的响应

您使用的是旧的 webPreferences 语法,您的构造函数应该看起来像这样:)

mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    frame: true,
    webPreferences: {
        webSecurity: false
    }
});

only suitable for development

mainWindow = new BrowserWindow({width: 900, height: 680,webPreferences: {
nodeIntegration: true,
webSecurity: false
}});

现在看来不可能,因为 webSecurity 不再控制 CORS。 (对于 v9+)

The webSecurity option controls the web security inside blink, but recently the control of CORS has been moved out of blink and thus the option no longer controls CORS.

https://github.com/electron/electron/issues/23664#issuecomment-692422997