HTML Web工作和jQuery Ajax调用 [英] HTML Web Worker and Jquery Ajax call
问题描述
我不知道我是否可以使用jQuery的网络工作者文件中。谷歌浏览器给了我这个错误:未捕获的ReferenceError:$没有定义。
I'm wondering if I can use jQuery inside the web worker file. Google Chrome gives me this error: "Uncaught ReferenceError: $ is not defined".
下面是code: 父文件:
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);
}
工人文件:
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) {
}
});
}
请帮忙,谢谢:)
推荐答案
没有您不可以。有非线程安全的组件或DOM进不去,你必须通过序列化对象传递一个线程和出具体的数据。所以,你必须非常努力,使你的code问题。 jQuery是一个JavaScript DOM库
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.
但是你可以在你的但是工作人员使用本机 XMLHtt prequest
。
But you can use a native XMLHttpRequest
in your worker however.
和,引入外部脚本通过页面不下去了剧本
标签:使用<一个href="https://developer.mozilla.org/En/Using_web_workers#Importing_scripts_and_libraries">importScripts()为您的员工档案。
And, importing external scripts does not go via the page with a script
tag : use importScripts() for that in your worker file.
这篇关于HTML Web工作和jQuery Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!