简体   繁体   English

如何在Web Worker中进行Ajax调用?

[英]How can I make an ajax call in a web worker?

I need to make ajax calls to a REST/JSON server from javascript. 我需要通过javascript对REST / JSON服务器进行ajax调用。 Both to pull down data to the browser and to push data up. 既可以将数据下拉到浏览器,也可以向上推送数据。 In a web worker. 在网络工作者中。

Because this is in a web worker JQuery cannot be used ( details here ). 因为这是在Web worker中,所以无法使用JQuery( 此处有详细信息 )。 And JQuery-hive is a library for managing web workers, not for using JQuery in a web worker (as far as I can tell from the documentation). 而且,JQuery-hive是用于管理Web Worker的库,而不是用于在Web Worker中使用JQuery的库(据我从文档中可以得知)。 So JQuery is not an option. 因此,JQuery不是一个选择。

In the limited environment of a web worker, how can I make ajax calls? 在网络工作者的有限环境中,如何进行Ajax调用?

Here's something to consider: jQuery is a abstraction tool: it can't do anything which isn't possible with basic JS. 这里有一些需要考虑的问题:jQuery是一种抽象工具:它无法完成基本JS无法完成的任何工作。 it might be easier with pure JS, it might be harder. 使用纯JS可能更容易,但是更难。 But it's not magical. 但这不是魔术。 It just provides an abstract interface which unifies the different implementations of javascript engines in different browsers. 它只是提供了一个抽象接口,该接口统一了不同浏览器中JavaScript引擎的不同实现。

In this case, jQuery is an abstraction for an XMLHttpRequest. 在这种情况下,jQuery是XMLHttpRequest的抽象。 You can find a guide on how to use this on every major javascript website: w3schools.com , MDN , MSDN ,... There are also many questions on stack overflow that explain how to do this. 您可以在每个主要的javascript网站上找到有关如何使用它的指南: w3schools.comMDNMSDN ,... 堆栈溢出中还有很多问题可以解释如何进行此操作。

Slightly off-topic, but this is also one of the reasons that I recommend people learning or developing javascript to first learn how to do the things manually, and only switching to a library once they've learned the basics. 稍微偏离主题,但这也是我建议人们学习或开发JavaScript的原因之一,该方法是首先学习手动操作方法,并且只有在学习了基础知识之后才切换到库。 A library is great for abstraction, but if you haven't learned WHAT is abstracted, you get into trouble once you end up in a situation where you don't have access to the abstraction. 库非常适合抽象,但是如果您不了解抽象的内容,那么一旦遇到无法访问抽象的情况,就会遇到麻烦。

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

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