简体   繁体   English

电子主流程上的jQuery

[英]jQuery on electron main process

How I can use jQuery on electron main process? 如何在电子主程序上使用jQuery?

It seems every example I find is for renderer process. 看来我找到的每个示例都是针对渲染器过程的。

Example I want to create a util that will be used by the main process, that will fetch data from an api using get. 示例我想创建一个将由主进程使用的util,它将使用get从api获取数据。

Then using $.get makes an error that get is not a function . 然后使用$.get会产生一个错误,指出get is not a function

Thanks. 谢谢。

jQuery is a JS library for the browser, eg DOM manipulating, etc. You shouldn't use that in the main process, since the main process is running in NodeJS . jQuery是浏览器的JS库,例如DOM操作等。您不应在主进程中使用它,因为主进程在NodeJS中运行。

It's hard to propose a solution without knowing more about your application. 不了解您的应用程序就很难提出解决方案。 If you need the data from the AJAX request in your main process, you can use NodeJS https package. 如果您在主流程中需要来自AJAX请求的数据,则可以使用NodeJS https包。 Example from Twilio blog : 来自Twilio博客的示例:

const https = require('https');

https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => {
  let data = '';

  // A chunk of data has been recieved.
  resp.on('data', (chunk) => {
    data += chunk;
  });

  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    console.log(JSON.parse(data).explanation);
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});

Edit: As @Hans-Koch mentioned, you probably shouldn't use jQuery in the renderer process either since one of it's main purpose is to normalize the API for DOM manipulation, AJAX, etc. and in Electron you only have to support Chromium. 编辑:正如@ Hans-Koch所述,您可能不应该在渲染器过程中使用jQuery,因为它的主要目的之一是为DOM操作,AJAX等规范化API,而在Electron中,您仅需支持Chromium。 If you want to make AJAX request you can use the XMLHttpRequest or some npm package which wraps it, eg xhr . 如果要发出AJAX请求,则可以使用XMLHttpRequest或包装它的npm软件包,例如xhr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM