在电子,什么是最好的方式来做Ajax请求?

我使用电子来创build一个桌面应用程序,现在我需要从一些远程API获取数据。

我可以在Renderer进程中使用fetch或Reqwest之类的东西吗?或者使用主进程上的任何http npm包(如Request),并使用Electron的IPC来来回转换数据。

那么最好的办法是什么?

我更喜欢原生的http和https包。 你可以直接在渲染过程中做一个请求。 以下是带error handling的示例请求。 也许有更好的解决scheme – 这只是我的处理。

// You Key - Value Pairs var postData = querystring.stringify({ key: "value" }); // Your Request Options var options = { host: "example.com", port: 443, path: "/path/to/api/endpoint", method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData) } }; // The Request var request = https.request(options, function(response) { response.on('data', function(chunk) { if (chunk) { var data = chunk.toString('utf8'); // holds your data } }); }).on("error", function(e) { // Some error handling }); //optionally Timeout Handling request.on('socket', function(socket) { socket.setTimeout(5000); socket.on('timeout', function() { request.abort(); }); }); request.write(postData); request.end();