文章源自略懂百科-http://wswcn.cn/93896.html
在这篇文章中,我们将介绍一些用于AJAX调用的最好的JS库,包括jQuery,Axios和Fetch。欢迎查看代码示例!文章源自略懂百科-http://wswcn.cn/93896.html
AJAX是用来对服务器进行异步HTTP调用的一系列web开发技术客户端框架。 AJAX即Asynchronous JavaScript and XML(异步JavaScript和XML)。AJAX曾是web开发界的一个常见名称,许多流行的JavaScript小部件都是使用AJAX构建的。例如,有些特定的用户交互(如按下按钮)会异步调用到服务器,服务器会检索数据并将其返回给客户端——所有这些都不需要重新加载网页。文章源自略懂百科-http://wswcn.cn/93896.html
文章源自略懂百科-http://wswcn.cn/93896.html
AJAX的现代化重新引入文章源自略懂百科-http://wswcn.cn/93896.html
JavaScript已经进化了,现在我们使用前端库和/或如React、Angular、Vue等框架构建了动态的网站。AJAX的概念也经历了重大变化,因为现代异步JavaScript调用涉及检索JSON而不是XML。有很多库允许你从客户端应用程序对服务器进行异步调用。有些进入到浏览器标准,有些则有很大的用户基础,因为它们不但灵活而且易于使用。有些支持promises,有些则使用回调。在本文中,我将介绍用于从服务器获取数据的前5个AJAX库。文章源自略懂百科-http://wswcn.cn/93896.html
Fetch API文章源自略懂百科-http://wswcn.cn/93896.html
Fetch API是XMLHttpRequest的现代替代品,用于从服务器检索资源。与XMLHttpRequest不同的是,它具有更强大的功能集和更有意义的命名。基于其语法和结构,Fetch不但灵活而且易于使用。但是,与其他AJAX HTTP库区别开来的是,它具有所有现代Web浏览器的支持。Fetch遵循请求-响应的方法,也就是说,Fetch提出请求并返回解析到Response对象的promise。文章源自略懂百科-http://wswcn.cn/93896.html
你可以传递Request对象来获取,或者,也可以仅传递要获取的资源的URL。下面的示例演示了使用Fetch创建简单的GET请求。文章源自略懂百科-http://wswcn.cn/93896.html
fetch(https://www.example.com, {文章源自略懂百科-http://wswcn.cn/93896.html
method: get文章源自略懂百科-http://wswcn.cn/93896.html
})文章源自略懂百科-http://wswcn.cn/93896.html
.then(response => response.json())文章源自略懂百科-http://wswcn.cn/93896.html
.then(jsonData => console.log(jsonData))文章源自略懂百科-http://wswcn.cn/93896.html
.catch(err => {文章源自略懂百科-http://wswcn.cn/93896.html
//error block文章源自略懂百科-http://wswcn.cn/93896.html
})文章源自略懂百科-http://wswcn.cn/93896.html
正如你所看到的,Fetch的then方法返回了一个响应对象,你可以使用一系列的then 进行进一步的操作。我使用.json() 方法将响应转换为JSON并将其输出到控制台。文章源自略懂百科-http://wswcn.cn/93896.html
假如你需要POST表单数据或使用Fetch创建AJAX文件上传,将会怎么样?此时,除了Fetch之外,你还需要一个输入表单,并使用FormData库来存储表单对象。文章源自略懂百科-http://wswcn.cn/93896.html
varinput = document.querySelector(input[type="file"])文章源自略懂百科-http://wswcn.cn/93896.html
vardata =newFormData()文章源自略懂百科-http://wswcn.cn/93896.html
data.append(file, input.files[0])文章源自略懂百科-http://wswcn.cn/93896.html
data.append(user, blizzerand)文章源自略懂百科-http://wswcn.cn/93896.html
fetch(/avatars, {文章源自略懂百科-http://wswcn.cn/93896.html
method: POST,文章源自略懂百科-http://wswcn.cn/93896.html
body: data文章源自略懂百科-http://wswcn.cn/93896.html
})文章源自略懂百科-http://wswcn.cn/93896.html
你可以在官方的Mozilla web文档中阅读更多关于Fetch API的信息。文章源自略懂百科-http://wswcn.cn/93896.html
Axios文章源自略懂百科-http://wswcn.cn/93896.html
Axios是一个基于XMLHttpRequest而构建的现代JavaScript库,用于进行AJAX调用。它允许你从浏览器和服务器发出HTTP请求。此外,它还支持ES6原生的Promise API。Axios的其他突出特点包括:文章源自略懂百科-http://wswcn.cn/93896.html
拦截请求和响应。使用promise转换请求和响应数据。自动转换JSON数据。取消实时请求。文章源自略懂百科-http://wswcn.cn/93896.html
要使用Axios,你需要先安装它。文章源自略懂百科-http://wswcn.cn/93896.html
npm install axios文章源自略懂百科-http://wswcn.cn/93896.html
下面是一个演示Axios行动的基本例子。文章源自略懂百科-http://wswcn.cn/93896.html
// Make a request for a user with a given ID文章源自略懂百科-http://wswcn.cn/93896.html
axios.get(/user?ID=12345)文章源自略懂百科-http://wswcn.cn/93896.html
.then(function(response) {文章源自略懂百科-http://wswcn.cn/93896.html
console.log(response);文章源自略懂百科-http://wswcn.cn/93896.html
})文章源自略懂百科-http://wswcn.cn/93896.html
.catch(function(error) {文章源自略懂百科-http://wswcn.cn/93896.html
console.log(error);文章源自略懂百科-http://wswcn.cn/93896.html
});文章源自略懂百科-http://wswcn.cn/93896.html
与Fetch相比,Axios的语法更简单。让我们做一些更复杂的事情,比如我们之前使用Fetch创建的AJAX文件上传器。文章源自略懂百科-http://wswcn.cn/93896.html
vardata =newFormData();文章源自略懂百科-http://wswcn.cn/93896.html
data.append(foo, bar);文章源自略懂百科-http://wswcn.cn/93896.html
data.append(file, document.getElementById(file).files[0]);文章源自略懂百科-http://wswcn.cn/93896.html
varconfig = {文章源自略懂百科-http://wswcn.cn/93896.html
onUploadProgress:function(progressEvent) {文章源自略懂百科-http://wswcn.cn/93896.html
varpercentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );文章源自略懂百科-http://wswcn.cn/93896.html
}文章源自略懂百科-http://wswcn.cn/93896.html
};文章源自略懂百科-http://wswcn.cn/93896.html
axios.put(/upload/server, data, config)文章源自略懂百科-http://wswcn.cn/93896.html
.then(function(res) {文章源自略懂百科-http://wswcn.cn/93896.html
output.className = container;文章源自略懂百科-http://wswcn.cn/93896.html
output.innerHTML = res.data;文章源自略懂百科-http://wswcn.cn/93896.html
})文章源自略懂百科-http://wswcn.cn/93896.html
.catch(function(err) {文章源自略懂百科-http://wswcn.cn/93896.html
output.className = container text-danger;文章源自略懂百科-http://wswcn.cn/93896.html
output.innerHTML = err.message;文章源自略懂百科-http://wswcn.cn/93896.html
});文章源自略懂百科-http://wswcn.cn/93896.html
Axios更具可读性。Axios也非常受React和Vue等现代库的欢迎。文章源自略懂百科-http://wswcn.cn/93896.html
jQuery文章源自略懂百科-http://wswcn.cn/93896.html
jQuery曾经是JavaScript中的一个前线库,用于处理从AJAX调用到操纵DOM内容的所有事情。虽然随着其他前端库的冲击,其相关性有所降低,但你仍然可以使用jQuery来进行异步调用。文章源自略懂百科-http://wswcn.cn/93896.html
如果你之前使用过jQuery,那么这可能是最简单的解决方案。但是,你将不得不导入整个jQuery库以使用$.ajax方法。虽然这个库有特定于域的方法,例如$.getJSON,$.get和$.post,但是其语法并不像其他的AJAX库那么简单。以下代码用于编写基本的GET请求。文章源自略懂百科-http://wswcn.cn/93896.html
$.ajax({文章源自略懂百科-http://wswcn.cn/93896.html
url: /users,文章源自略懂百科-http://wswcn.cn/93896.html
type: "GET",文章源自略懂百科-http://wswcn.cn/93896.html
dataType: "json",文章源自略懂百科-http://wswcn.cn/93896.html
success:function(data) {文章源自略懂百科-http://wswcn.cn/93896.html
console.log(data);文章源自略懂百科-http://wswcn.cn/93896.html
}文章源自略懂百科-http://wswcn.cn/93896.html
fail:function() {文章源自略懂百科-http://wswcn.cn/93896.html
console.log("Encountered an error")文章源自略懂百科-http://wswcn.cn/93896.html
}文章源自略懂百科-http://wswcn.cn/93896.html
});文章源自略懂百科-http://wswcn.cn/93896.html
jQuery好的地方在于,如果你有疑问,那么你可以找到大量的支持和文档。我发现了很多使用FormData()和jQuery进行AJAX文件上传的例子。下面是最简单的方法:文章源自略懂百科-http://wswcn.cn/93896.html
varformData =newFormData();文章源自略懂百科-http://wswcn.cn/93896.html
formData.append(file, $(file)[0].files[0]);文章源自略懂百科-http://wswcn.cn/93896.html
$.ajax({文章源自略懂百科-http://wswcn.cn/93896.html
url : upload.php,文章源自略懂百科-http://wswcn.cn/93896.html
type : POST,文章源自略懂百科-http://wswcn.cn/93896.html
data : formData,文章源自略懂百科-http://wswcn.cn/93896.html
processData: false, // tell jQuery not to process the data文章源自略懂百科-http://wswcn.cn/93896.html
contentType: false, // tell jQuery not to set contentType文章源自略懂百科-http://wswcn.cn/93896.html
success :function(data) {文章源自略懂百科-http://wswcn.cn/93896.html
console.log(data);文章源自略懂百科-http://wswcn.cn/93896.html
alert(data);文章源自略懂百科-http://wswcn.cn/93896.html
}文章源自略懂百科-http://wswcn.cn/93896.html
});文章源自略懂百科-http://wswcn.cn/93896.html
SuperAgent文章源自略懂百科-http://wswcn.cn/93896.html
SuperAgent是一个轻量级和渐进式的AJAX库,更侧重于可读性和灵活性。SuperAgent还拥有一个温和的学习曲线,不像其他库。它有一个针对Node.js API相同的模块。SuperAgent有一个接受GET、POST、PUT、DELETE和HEAD等方法的请求对象。然后你可以调用.then(),.end()或新的.await()方法来处理响应。例如,以下代码为使用SuperAgent的简单GET请求。文章源自略懂百科-http://wswcn.cn/93896.html
request文章源自略懂百科-http://wswcn.cn/93896.html
.post(/api/pet)文章源自略懂百科-http://wswcn.cn/93896.html
.send({ name: Manny, species: cat })文章源自略懂百科-http://wswcn.cn/93896.html
.set(X-API-Key, foobar)文章源自略懂百科-http://wswcn.cn/93896.html
.set(Accept, application/json)文章源自略懂百科-http://wswcn.cn/93896.html
.then(function(res) {文章源自略懂百科-http://wswcn.cn/93896.html
alert(yay got + JSON.stringify(res.body));文章源自略懂百科-http://wswcn.cn/93896.html
});文章源自略懂百科-http://wswcn.cn/93896.html
如果你想要做更多的事情,比如使用此AJAX库上传文件,那该怎么做呢? 同样超级easy。文章源自略懂百科-http://wswcn.cn/93896.html
request文章源自略懂百科-http://wswcn.cn/93896.html
.post(/upload)文章源自略懂百科-http://wswcn.cn/93896.html
.field(user[name], Tobi)文章源自略懂百科-http://wswcn.cn/93896.html
.field(user[email], tobi@learnboost.com)文章源自略懂百科-http://wswcn.cn/93896.html
.field(friends[], [loki, jane])文章源自略懂百科-http://wswcn.cn/93896.html
.attach(image, path/to/tobi.png)文章源自略懂百科-http://wswcn.cn/93896.html
.then(callback);文章源自略懂百科-http://wswcn.cn/93896.html
如果你有兴趣了解更多关于SuperAgent的信息,那么它们有一系列很不错的文档来帮助你开始这个旅程。文章源自略懂百科-http://wswcn.cn/93896.html
Request——简化的HTTP客户端文章源自略懂百科-http://wswcn.cn/93896.html
Request库是进行HTTP调用最简单的方法之一。结构和语法与在Node.js中处理请求的方式非常相似。目前,该项目在GitHub上有18K个星,值得一提的是,它是可用的最流行的HTTP库之一。 下面是一个例子:文章源自略懂百科-http://wswcn.cn/93896.html
varrequest = require(request);文章源自略懂百科-http://wswcn.cn/93896.html
request(http://www.google.com,function(error, response, body) {文章源自略懂百科-http://wswcn.cn/93896.html
console.log(error:, error); // Print the error if one occurred文章源自略懂百科-http://wswcn.cn/93896.html
console.log(statusCode:, response && response.statusCode); // Print the response status code if a response was received文章源自略懂百科-http://wswcn.cn/93896.html
console.log(body:, body); // Print the HTML for the Google homepage.文章源自略懂百科-http://wswcn.cn/93896.html
});文章源自略懂百科-http://wswcn.cn/93896.html
结论文章源自略懂百科-http://wswcn.cn/93896.html
从客户端进行HTTP调用在十年前可不是一件容易的事。前端开发人员不得不依赖于难以使用和实现的XMLHttpRequest。现代的库和HTTP客户端使得用户交互、动画、异步文件上传等前端功能变得更加简单。文章源自略懂百科-http://wswcn.cn/93896.html
我个人最喜欢的是Axios,因为我觉得它更易读更赏心悦目。你也可以忠于Fetch,因为它文档化良好且有标准化的解决方案。文章源自略懂百科-http://wswcn.cn/93896.html
你个人最喜欢的AJAX库是哪个? 欢迎各位分享你的看法。文章源自略懂百科-http://wswcn.cn/93896.html
文章源自略懂百科-http://wswcn.cn/93896.html
评论