gpt4 book ai didi

jquery - HTML Web Worker 和 Jquery Ajax 调用

转载 作者:行者123 更新时间:2023-11-28 06:35:25 25 4
gpt4 key购买 nike

我想知道是否可以在 Web Worker 文件中使用 jQuery。 Google Chrome 给我这个错误:“Uncaught ReferenceError:$未定义”。

代码如下:父文件:

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,并且必须通过序列化对象将特定数据传入和传出线程。所以你必须非常努力地工作才能在你的代码中引起问题。 jQuery 是一个 JavaScript DOM 库

但是您可以在工作线程中使用 native XMLHttpRequest

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

关于jquery - HTML Web Worker 和 Jquery Ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com