当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript fetch接口案例解析

JavaScript fetch接口案例解析

2018年09月24日  | 移动技术网IT编程  | 我要评论

在 ajax 时代,进行 api 等网络请求都是通过 xmlhttprequest 或者封装后的框架进行网络请求。

现在产生的 fetch 框架简直就是为了提供更加强大、高效的网络请求而生,虽然在目前会有一点浏览器兼容的问题,但是当我们进行 hybrid app 开发的时候,如我之前介绍的 ionic 和 react native,都可以使用 fetch 进行完美的网络请求。

如果看网上的fetch教程,会首先对比xmlhttprequest和fetch的优劣,然后引出一堆看了很快会忘记的内容(本人记性不好)。因此,我写一篇关于fetch的文章,为了自己看着方便,毕竟工作中用到的也就是一些很基础的点而已。

fetch,说白了,就是xmlhttprequest的一种替代方案。如果有人问你,除了ajax获取后台数据之外,还有没有其他的替代方案?

这是你就可以回答,除了xmlhttprequest对象来获取后台的数据之外,还可以使用一种更优的解决方案fetch。

fetch的案例

下面我们来写第一个fetch获取后端数据的例子:

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html') // 返回一个promise对象
 .then((res)=>{
  return res.text() // res.text()是一个promise对象
 })
 .then((res)=>{
  console.log(res) // res是最终的结果
 })

get请求

get请求初步

完成了helloworld,这个时候就要来认识一下get请求如何处理了。

上面的helloworld中这是使用了第一个参数,其实fetch还可以提供第二个参数,就是用来传递一些初始化的信息。

这里如果要特别指明是get请求,就要写成下面的形式:

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html', {
  method: 'get'
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

get请求的参数传递

get请求中如果需要传递参数怎么办?这个时候,只能把参数写在url上来进行传递了。

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html?a=1&b=2', { // 在url中写上传递的参数
  method: 'get'
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

post请求

与get请求类似,post请求的指定也是在fetch的第二个参数中:

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html', {
  method: 'post' // 指定是post请求
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

post请求参数的传递

众所周知,post请求的参数,一定不能放在url中,这样做的目的是防止信息泄露。

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html', {
  method: 'post',
  body: new urlsearchparams([["foo", 1],["bar", 2]]).tostring() // 这里是请求对象
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

设置请求的头信息

在post提交的过程中,一般是表单提交,可是,经过查询,发现默认的提交方式是:content-type:text/plain;charset=utf-8,这个显然是不合理的。下面咱们学习一下,指定头信息:

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html', {
  method: 'post',
  headers: new headers({
   'content-type': 'application/x-www-form-urlencoded' // 指定提交方式为表单提交
  }),
  body: new urlsearchparams([["foo", 1],["bar", 2]]).tostring()
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

这个时候,在谷歌浏览器的network中查询,会发现,请求方式已经变成了content-type:application/x-www-form-urlencoded。

通过接口得到json数据

上面所有的例子中都是返回一个文本,那么除了文本,有没有其他的数据类型呢?肯定是有的,具体查询地址:body的类型

由于最常用的是json数据,那么下面就简单演示一下获取json数据的方式:

fetch('https://www.baidu.com/rec?platform=wise&ms=1&rset=rcmd&word=123&qid=11327900426705455986&rq=123&from=844b&baiduid=a1d0b88941b30028c375c79ce5ac2e5e%3afg%3d1&tn=&clientwidth=375&t=1506826017369&r=8255', { // 在url中写上传递的参数
  method: 'get',
  headers: new headers({
   'accept': 'application/json' // 通过头指定,获取的数据类型是json
  })
 })
 .then((res)=>{
  return res.json() // 返回一个promise,可以解析成json
 })
 .then((res)=>{
  console.log(res) // 获取json数据
 })

强制带cookie

默认情况下, fetch 不会从服务端发送或接收任何 cookies, 如果站点依赖于维护一个用户会话,则导致未经认证的请求(要发送 cookies,必须发送凭据头).

// 通过fetch获取百度的错误提示页面
fetch('https://www.baidu.com/search/error.html', {
  method: 'get',
  credentials: 'include' // 强制加入凭据头
 })
 .then((res)=>{
  return res.text()
 })
 .then((res)=>{
  console.log(res)
 })

总结

以上所述是小编给大家介绍的javascript fetch接口案例解析,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网