Me pregunto si puedo usar jQuery dentro del archivo de trabajo web. Google Chrome me da este error: "Unbeught ReferenceError: $ no está definido".Trabajador web HTML y Jquery Ajax llame al
Aquí está el código: El archivo principal:
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);
}
El archivo trabajador:
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) {
}
});
}
Por favor ayuda, gracias :)
importScripts ("jquery.js"); no puede funcionar: jQuery usa la variable 'ventana', que no es accesible para los trabajadores web. Pero puede usar otra biblioteca que hará el trabajo =) –
Para ser claros para los buscadores entrantes, es posible y a menudo beneficioso ejecutar solicitudes ajax de un 'Worker'. Simplemente no con 'jQuery' tradicional, ya que' jQuery' es una biblioteca de manipulación DOM y Web Workers tiene 'WorkerGlobalScope' y no' Window' y por lo tanto no tiene acceso al documento, o DOM. – buley