js fetch请求使用

痛定思痛。 2023-10-10 14:54 134阅读 0赞

文章目录

  • 1、fetch请求语法格式
    • 1.1、fetch请求options参数说明
    • 1.2、fetch请求response返回json数据
    • 1.3、fetch请求返回text文本数据解析
    • 1.4、fetch请求返回blob二进制流解析文件
  • 2、fetch请求示例
    • 2.1、fetch发送get请求,url参数
    • 2.2、fetch发送post请求,formdata参数
    • 2.3、fetch发送post请求,json参数
  • 参考链接

1、fetch请求语法格式

语法格式:

  1. 用于response返回数据格式是json格式
    fetch(url [,options]).then(rsp => {return rsp.json()}).then(data => {console.log(data)})
  2. 用于respnse返回数据格式为文本格式
    fetch(url [,options]).then(rsp => {return rsp.text()}).then(data => {console.log(data)})
  3. 用来请求文件返回二进制流
    fetch(url [,options]).then(rsp => {return rsp.blob()}).then(data => {console.log(data)})

1.1、fetch请求options参数说明

  1. 说明:
  2. urlfetch请求的url地址
  3. optionsrequest请求设置,部分参数说明
  4. options参数示例:
  5. {
  6. 'method': 'POST', // 请求方式
  7. // 'body': 'a=1&b=2&c=3', // url参数直接放入,content-type='application/x-www-form-urlencoded; charset=UTF-8'
  8. 'body': JSON.stringify({
  9. 'a':'1'}), // 请求体为json对象:JSON.stringify({'a':'1'}),content-type='application/json;chartset-UTF-8'
  10. 'headers': {
  11. "accept": "*/*",
  12. "sec-fetch-dest": "empty",
  13. "sec-fetch-mode": "cors",
  14. "sec-fetch-site": "none",
  15. 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' // 内容类型
  16. }
  17. }
  18. options参数组合示例:
  19. 1. 请求方式为POST请求、且内容类型为application/x-www-form-urlencodedbody请求体内容为FormData对象:
  20. var formData = new FormData()
  21. formData.append('a','1')
  22. formData.append('b','2')
  23. formData.append('c','3')
  24. formData.append('j',{
  25. 'k':'v'})
  26. // ====> 结果
  27. // a=1&b=2&c=3&j={'k':'v'}
  28. 2. 请求方式为POST请求、且内容类型为application/jsonbody请求体内容为JSON字符串:
  29. JSON.stringify({
  30. 'a':'1'})

1.2、fetch请求response返回json数据

  1. fetch('https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png').then(rsp => {
  2. return rsp.json()
  3. }).then(d => {
  4. console.log(d) // d: 返回json数据
  5. })

1.3、fetch请求返回text文本数据解析

  1. fetch('https://www.baidu.com').then(rsp => {
  2. return rsp.text()}).then(d => {
  3. console.log(d) // d:返回html文本
  4. var dom = new DOMParse()
  5. let doc = dom.parseFromString(d, 'html/text') // 返回一个document对象
  6. // to do other dom element...
  7. })

1.4、fetch请求返回blob二进制流解析文件

  1. fetch('https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png').then(rsp => {
  2. return rsp.blob()}).then(d => {
  3. var blob = d
  4. // 创建隐藏的可下载链接
  5. const a = document.createElement('a')
  6. a.style.display = 'none'
  7. a.href = URL.createObjectURL(blob)
  8. // 保存下来的文件名
  9. a.download = 'my_img' + '.jpg'
  10. document.body.appendChild(a)
  11. a.click()
  12. // 移除元素
  13. document.body.removeChild(a)
  14. })

2、fetch请求示例

2.1、fetch发送get请求,url参数

  1. fetch('http://localhost:8080/api/?a=1&b=2&c=3', {
  2. 'method': 'GET', // 请求方式
  3. 'body': null, // GET请求不存在请求体body
  4. 'headers': {
  5. "accept": "*/*",
  6. "sec-fetch-dest": "empty",
  7. "sec-fetch-mode": "cors",
  8. "sec-fetch-site": "none",
  9. 'content-type': 'application/json, text/plain, */*' // 请求内容类型
  10. }
  11. }).then(rsp => {
  12. return rsp.json()}).then(data => {
  13. console.log(data)
  14. })

2.2、fetch发送post请求,formdata参数

  1. fetch('http://localhost:8080/api/xxx', {
  2. 'method': 'POST', // 请求方式
  3. 'body': 'a=1&b=2&c=3', // POST请求请求体可有可无,参数也可以放到url上。表单参数必须放到请求体上,注意contentType类型
  4. 'headers': {
  5. "accept": "*/*",
  6. "sec-fetch-dest": "empty",
  7. "sec-fetch-mode": "cors",
  8. "sec-fetch-site": "none",
  9. 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' // 请求内容类型
  10. }
  11. }).then(rsp => {
  12. return rsp.json()}).then(data => {
  13. console.log(data)
  14. })

2.3、fetch发送post请求,json参数

  1. // 需要注意的就是参数:参数中间不能有空格,中文参数要使用encode编码
  2. // 还要注意encodeURIComponent和encodeURI编码的区别(尤其是参数部分冒号的转码,具体可以看下参考链接)
  3. let body = encodeURIComponent(JSON.stringify({
  4. 'a':'1','b':'2','c':'3'}))
  5. fetch('http://localhost:8080/api/xxx', {
  6. 'method': 'POST', // 请求方式
  7. 'body': body, // POST请求发送json参数,json参数必须放到请求体上,通过http输入流读取。注意contentType类型
  8. 'headers': {
  9. "accept": "*/*",
  10. "sec-fetch-dest": "empty",
  11. "sec-fetch-mode": "cors",
  12. "sec-fetch-site": "none",
  13. 'content-type': 'application/json; charset=UTF-8' // 请求内容类型
  14. }
  15. }).then(rsp => {
  16. return rsp.json()}).then(data => {
  17. console.log(data)
  18. })

参考链接

encodeURI()、encodeURIComponent()区别及使用场景

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读

    相关 fetch请求详解

    最新更新时间:2019年06月04日16:18:30 [`《猛戳-查看我的博客地图-总有你意想不到的惊喜》`][-_-] > 本文内容:fetch相关 概述