当前位置: 首页 > wzjs >正文

建网站手机版江苏seo排名

建网站手机版,江苏seo排名,旅游的网站怎么做,苏州网站推广建设1. Fetch API 简介 fetch 是 ES6 提供的基于 Promise 的 API,用于发送 HTTP 请求并处理服务器响应数据。与传统的 XMLHttpRequest 相比,fetch 语法更加简洁,使用 Promise 进行异步处理,避免了回调地狱。 1.1 fetch() 的基本用法 …

1. Fetch API 简介

  fetch 是 ES6 提供的基于 Promise 的 API,用于发送 HTTP 请求并处理服务器响应数据。与传统的 XMLHttpRequest 相比,fetch 语法更加简洁,使用 Promise 进行异步处理,避免了回调地狱。

1.1 fetch() 的基本用法

fetch(url, options) 返回一个 Promise,其中:

  • url:请求的地址。

  • options(可选):一个对象,用于设置请求方法、请求头、请求体等。

示例:

fetch('http://127.0.0.1/get').then(response => response.json()) // 解析 JSON 格式的响应数据.then(data => console.log("get.data:", data)).catch(error => console.log("get.error:", error.message)).finally(() => console.log("get.finally"));

2. 发送不同类型的 HTTP 请求

2.1 发送 GET 请求

GET 请求不需要 body,通常用于获取数据。

fetch('http://127.0.0.1/get').then(response => response.json()).then(data => console.log("get.data:", data)).catch(error => console.log("get.error:", error.message)).finally(() => console.log("get.finally"));

2.2 发送 POST 请求(表单数据)

当需要提交数据时,可以使用 POST 方法,并在 headers 指定 Content-Type

fetch('http://127.0.0.1/post', {method: 'POST',headers: {'Content-Type': 'application/x-www-form-urlencoded'},body: new URLSearchParams({name: 'theodore',web: 'https://blog.csdn.net/Theodore_1022'})
})
.then(response => response.json())
.then(data => console.log("post.data:", data))
.catch(error => console.log("post.error:", error.message))
.finally(() => console.log("post.finally"));

2.3 发送 POST 请求(JSON 数据)

提交 JSON 数据时,Content-Type 需要设置为 application/json,并使用 JSON.stringify() 处理 body

fetch('http://127.0.0.1/postJson', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({name: 'theodore',web: 'https://blog.csdn.net/Theodore_1022'})
})
.then(response => response.json())
.then(data => console.log("postJson.data:", data))
.catch(error => console.log("postJson.error:", error.message))
.finally(() => console.log("postJson.finally"));

3. 处理 Fetch 响应

fetch 返回的 Promise 解析后得到的是一个 Response 对象,需要进一步解析数据。

fetch('http://127.0.0.1/get').then(response => {if (!response.ok) {throw new Error(`HTTP 错误!状态码: ${response.status}`);}return response.json();}).then(data => console.log("get.data:", data)).catch(error => console.log("get.error:", error.message));

3.1 解析不同格式的响应数据

fetchResponse 对象提供多个方法来解析数据:

  • response.json():解析 JSON 格式数据。

  • response.text():解析文本数据。

  • response.blob():解析二进制数据,如图片、音视频。

  • response.arrayBuffer():解析为 ArrayBuffer,用于处理二进制流数据。

示例:

fetch('http://127.0.0.1/data').then(response => response.text()).then(text => console.log("text data:", text));

4. 处理超时请求

fetch 默认没有超时机制,可以使用 AbortController 来手动终止请求。

const controller = new AbortController();
const signal = controller.signal;setTimeout(() => controller.abort(), 5000); // 5 秒后取消请求fetch('http://127.0.0.1/timeout', { signal }).then(response => response.json()).then(data => console.log("data:", data)).catch(error => {if (error.name === 'AbortError') {console.log('请求超时,已取消');} else {console.log('请求失败:', error.message);}});

5. 处理跨域问题

如果请求跨域,需要服务器配置 CORS(跨域资源共享)。

  • 服务器返回 Access-Control-Allow-Origin: * 允许跨域访问。

  • 如果涉及 credentials(如 cookies),需要设置 fetchcredentials

fetch('http://127.0.0.1/protected', {credentials: 'include' // 允许携带 cookies
})
.then(response => response.json())
.then(data => console.log("data:", data));

6. 结语

  fetch 是 ES6 现代 Web 开发中常用的 API,它相比传统 XMLHttpRequest 更加简洁且基于 Promise,使得异步代码更加优雅。掌握 fetch,可以更轻松地处理 HTTP 请求,提高前端开发效率。

http://www.dtcms.com/wzjs/257433.html

相关文章:

  • 如何查看一个网站的域名解析网络营销的收获与体会
  • 网站预算表怎么做超级外链发布工具
  • 郑州疫情引高度关注官方回应问题合肥seo排名收费
  • 网上课程网站外贸seo软件
  • 顺德网站建设公司信息seo搜索引擎优化步骤
  • 做教育视频网站用什么平台好什么叫营销
  • 泉州网站建设开发泉州百度竞价推广
  • 软件免费下载的网站大全网站优化方案模板
  • 站酷设计官方网站矿产网站建设价格
  • 推荐设计感强的网站百度拍照搜索
  • 个人博客网页模板seo专员是指什么意思
  • 网站开发与建设方向品牌seo主要做什么
  • 如何制作微信网站磁力猫最好磁力搜索引擎
  • 广州东莞高坎疫情最新消息seo团队管理系统
  • 怎么做弹幕视频网站chatgpt网址
  • 哈尔滨免费模板建站西地那非片能延时多久
  • 吉安市建设技术培训中心网站免费网站服务器安全软件下载
  • 深圳做网站优化工资多少seo零基础入门教程
  • 萧山做网站的企业做网站怎么做
  • gstatic wordpress珠海关键词优化软件
  • 文字图片在线制作生成器网站优化哪个公司好
  • 网站建设服务电话爱站网综合查询
  • 简述营销型网站开发流程图今日头条新闻最新消息
  • wordpress网站源码分享百度热度榜搜索趋势
  • 龙岗网站建设推广报价北京互联网公司有哪些
  • 网站域名都需要备案吗seo排名优化方法
  • 中国少数民族网站建设徐州百度seo排名优化
  • flask做的网站有哪些百度问答库
  • 郑州做网站推广外包查询关键词排名软件
  • 用Html5做网站seo综合查询工具下载