HTML Web Worker 和 Jquery Ajax 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4838883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:01:25  来源:igfitidea点击:

HTML Web Worker and Jquery Ajax call

jqueryajaxhtmlweb-worker

提问by Tri

I'm wondering if I can use jQuery inside the web worker file. Google Chrome gives me this error: "Uncaught ReferenceError: $ is not defined".

我想知道我是否可以在 web worker 文件中使用 jQuery。谷歌浏览器给了我这个错误:“Uncaught ReferenceError: $ is not defined”。

Here is the code: The parent file:

这是代码: 父文件:

var loader = new Worker(BASE_URL + "js/rss_loader_worker.js");
// Ask the worker to start loading the RSS from the server
loader.postMessage("loadRss");
// When receive the response from the server
loader.onmessage = function (event) {
  console.log(event.data);
}

The worker file:

工人档案:

onmessage = function (event) {
  if (event.data === "loadRss") {
    loadRss();
  }
}

/**
 * This function handles the AJAX request to the server side
 * then pass the content to the view page
 * @param none
 * @return html text
 */
loadRss = function () {
  $.ajax({
    data: {city: CITY_LOCATION},
    url: BASE_URL + "/getfeeds",
    onsucess: function (data) {

    }
  });
}

Please help, thank you :)

请帮忙,谢谢:)

采纳答案by Caspar Kleijne

No you cannot. There's no access to non-thread safe components or the DOM and you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code. jQuery is a JavaScript DOM library.

不,你不能。无法访问非线程安全组件或 DOM,您必须通过序列化对象将特定数据传入和传出线程。所以你必须非常努力地在你的代码中引起问题。jQuery 是一个 JavaScript DOM 库

But you can use a native XMLHttpRequestin your worker however.

但是您可以XMLHttpRequest在您的工作人员中使用本机。

And, importing external scripts does not go via the page with a scripttag : use importScripts()for that in your worker file.

而且,导入外部脚本不会通过带有script标签的页面:在您的工作文件中使用importScripts()

回答by Sherzod

Here's what I found:

这是我发现的:

You can load external script files or libraries into a worker with the importScripts()function.

您可以使用该importScripts()函数将外部脚本文件或库加载到 worker 中 。

http://www.html5rocks.com/en/tutorials/workers/basics/#toc-enviornment-loadingscripts

http://www.html5rocks.com/en/tutorials/workers/basics/#toc-enviornment-loadingscripts

importScripts('script1.js');
importScripts('script2.js');

or

或者

importScripts('script1.js', 'script2.js');

Although, you cannot load jQuery, because jQuery requires DOM access, which web workers don't have.

虽然,您无法加载 jQuery,因为 jQuery 需要 DOM 访问权限,而 Web 工作者没有。

回答by Sherzod

Since web workers are in external files, they do not have access to the following JavaScript objects:

由于 Web Worker 位于外部文件中,因此他们无法访问以下 JavaScript 对象:

  • The window object
  • The document object
  • The parent object
  • 窗口对象
  • 文档对象
  • 父对象

So you can't use $ inside worker file. Better you can use traditional AJAX something like this

所以你不能在工作文件中使用 $ 。更好的是你可以使用像这样的传统 AJAX

if (window.XMLHttpRequest)
{
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else
{
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

Reference at http://www.w3schools.com/html/html5_webworkers.asp

参考http://www.w3schools.com/html/html5_webworkers.asp

回答by Fred

jQuery is mostly for working with the DOM and working with web pages. So it is not so suitable for Web Workers that do not have access to the DOM.

jQuery 主要用于处理 DOM 和处理网页。所以它不太适合无法访问 DOM 的 Web Workers。

You might want to use a utility library instead of a DOM library, such as underscore.js, or perhaps someone ought to make a jQuery Worker library, a stripped down light version of jQuery without all the DOM manipulation functionality, just keeping the utility functions.

您可能想要使用实用程序库而不是 DOM 库,例如underscore.js,或者也许有人应该制作一个 jQuery Worker 库,这是一个精简版的 jQuery,没有所有 DOM 操作功能,只保留实用程序功能.

回答by jokeyrhyme

The execution environment in Node.JS also lacks a native DOM implementation. I think it's fair to say that Node.JS and HTML5 Web Workers share certain restrictions.

Node.JS 中的执行环境也缺乏原生的 DOM 实现。我认为可以公平地说 Node.JS 和 HTML5 Web Workers 共享某些限制。

There are ways to simulate a DOM implementation for the purpose of using jQuery in Node.JS. If you still want to use jQuery in Web Workers, I think you should search for the Node.JS solutions and see if they apply.

为了在 Node.JS 中使用 jQuery,有多种方法可以模拟 DOM 实现。如果你还想在 Web Workers 中使用 jQuery,我认为你应该搜索 Node.JS 解决方案,看看它们是否适用。

回答by Alexandre