AJAX/HTTP网络请求库对比
Posted misstakau
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AJAX/HTTP网络请求库对比相关的知识,希望对你有一定的参考价值。
Networking in JavaScript falls into 2 categories: AJAX (browser) and HTTP clients (server).
JavaScript中的网络分为两大类:AJAX(浏览器)和HTTP客户端(服务器)。
Sometimes you'll only need one of these, sometimes you'll want both (in an isomorphic/universal app for instance).
有时候你只需要它们之中的一种,有时候两者都需要(比如在同构/通用的应用程序中)。
Either way, you'll probably want a nice concise syntax. Most developers find the XMLHttpRequest API a little too verbose for their taste.
不管怎样,你都会希望拥有简洁的语法来编写,大多数的开发者都发现XMLHttpRequest的API太冗长了。
Many developers use jQuery, but if you only need the AJAX features, loading the whole library may seem like a waste.
很多开发者选择使用jQuery,但是如果你只用AJAX却引入了整个库,这似乎太浪费了。
I've put together a list to help you pick the perfect JavaScript networking library for your project. Afterwards I'll suggest a library for a few specific scenarios.
我整理了一个列表来帮助你为你的项目选择一个最合适的JavaScript网络请求库。之后,我会举几个具体的场景来推荐相应库。
Chrome & Firefox:单独列出这两个浏览器是因为它们支持fetch():caniuse.com/fetch
Native:意思是原生支持,你可以直接使用而不需要为此引入一个库
Single Purpose:意思是这个库或者这个技术仅仅用于AJAX/HTTP网络通信,没有其他功能
fetch() is the new standard. It's already supported in Chrome and Firefox - so if don't need IE or Safari support you can use it today without any library at all. I suggest you start here.
fetch()是新的标准。它已经在Chrome和Firefox中原生支持了,你完全不用引入任何库就能直接使用它了 —— 所有如果你的项目不需要考虑IE和Safari的话,我建议你马上就开始使用它。
If you need support for more browsers I suggest GitHub's Fetch polyfill. You get to use the new standard now, and eventually you can delete the polyfill library when more browsers support Fetch.
如果你需要支持更多浏览器的话我推荐GitHub的Fetch polyfill.你可以使用这些新特性,等到浏览器原生支持了你再来删掉这些库;
If you're using node.js you can still use Fetch, but you need the node-fetch library.
如果你使用node.js,你仍然可以使用Fetch,但是你需要node-fetch库;
If your code is universal there's a Fetch library for that too: isomorphic-fetch. It wraps both node-fetch and the Fetch polyfill.
如果你的代码是通用的,同样也有一个库可选:isomorphic-fetch,它包含了node-fetch和Fetch polyfill;
If you are already using jQuery, just use $.get or $.ajax. No additional libraries needed, and you'll have good support in most browsers. Using the Fetch polyfill would add unnecessary k-weight to your code.
如果你已经使用jQuery,你只需要使用$.get或$.ajax即可。不必额外引入新的库,而且你还将很好的支持绝大多数的浏览器,如果使用Fetch polyfill的话反而造成你的项目不必要的负担。
以上是关于AJAX/HTTP网络请求库对比的主要内容,如果未能解决你的问题,请参考以下文章