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

建设网站最好的2023疫情最新消息今天

建设网站最好的,2023疫情最新消息今天,衡水网站建设维护,前端开发是什么专业在JavaScript中,fetch 是用于发起HTTP请求的API,你提到的这些格式(form - data、x - www - form - urlencoded、json 等)主要用于设置 fetch 请求的 body 部分。以下是它们的使用示例: 1. none 当不需要发送请求体时…

在JavaScript中,fetch 是用于发起HTTP请求的API,你提到的这些格式(form - datax - www - form - urlencodedjson 等)主要用于设置 fetch 请求的 body 部分。以下是它们的使用示例:

1. none

当不需要发送请求体时使用,例如GET请求:

fetch('https://example.com/api/data',  { method: 'GET' 
}) 
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

2. form - data

用于发送表单数据,通常用于上传文件或包含多种数据类型的表单:

const formData = new FormData(); 
formData.append('username',  'JohnDoe'); 
formData.append('password',  'secret'); 
formData.append('avatar',  fileInput.files[0]);  // 假设fileInput是一个<input type="file">元素 fetch('https://example.com/api/submit',  { method: 'POST', body: formData 
}) 
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

3. x - www - form - urlencoded

将数据编码为URL查询字符串格式,常用于传统的表单提交:

const data = { username: 'JohnDoe', password: 'secret' 
}; 
const formBody = new URLSearchParams(); 
for (const key in data) { formBody.append(key,  data[key]); 
} fetch('https://example.com/api/login',  { method: 'POST', headers: { 'Content - Type': 'application/x - www - form - urlencoded' }, body: formBody.toString()  
}) 
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

4. json

用于发送JSON格式的数据,这是现代Web API中最常见的方式:

const data = { username: 'JohnDoe', password: 'secret' 
}; fetch('https://example.com/api/login',  { method: 'POST', headers: { 'Content - Type': 'application/json' }, body: JSON.stringify(data)  
}) 
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

5. xml

用于发送XML格式的数据,虽然不如JSON流行,但在一些特定场景下仍有使用:

const xmlData = ` 
<user> <username>JohnDoe</username> <password>secret</password> 
</user> 
`; fetch('https://example.com/api/submit',  { method: 'POST', headers: { 'Content - Type': 'application/xml' }, body: xmlData 
}) 
.then(response => response.text())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

6. raw

发送原始数据,可以是字符串、Blob等:

const rawData = 'This is raw data'; fetch('https://example.com/api/submit',  { method: 'POST', headers: { 'Content - Type': 'text/plain' }, body: rawData 
}) 
.then(response => response.text())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

7. binary

用于发送二进制数据,例如文件内容。通常使用 Blob 或 ArrayBuffer

const file = fileInput.files[0];  // 假设fileInput是一个<input type="file">元素 fetch('https://example.com/api/upload',  { method: 'POST', body: file 
}) 
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

8. GraphQL

用于发送GraphQL查询或突变请求,通常将查询字符串作为请求体:

const graphQLQuery = ` 
query { user(id: "123") { username email } 
} 
`; fetch('https://example.com/graphql',  { method: 'POST', headers: { 'Content - Type': 'application/json' }, body: JSON.stringify({  query: graphQLQuery }) 
}) 
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

9. msgpack

MessagePack是一种高效的二进制序列化格式。使用时需要先安装 msgpack - lite 等库:

const msgpack = require('msgpack - lite'); 
const data = { username: 'JohnDoe', password: 'secret' 
}; 
const packedData = msgpack.encode(data);  fetch('https://example.com/api/submit',  { method: 'POST', headers: { 'Content - Type': 'application/msgpack' }, body: packedData 
}) 
.then(response => response.arrayBuffer())  
.then(buffer => msgpack.decode(new  Uint8Array(buffer))) 
.then(data => console.log(data))  
.catch(error => console.error('Error:',  error)); 

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

相关文章:

  • 网站排名提高惠州seo计费管理
  • 做wap网站能火吗广告投放网站
  • 盱眙网站建设企业网站排名优化公司
  • 网站的程序和数据库怎么做的百度优化大师
  • 网站建设团队技术介绍什么是新媒体运营
  • 个人可以建设哪些网站电商网站建设制作
  • 百度网站首页收录seo观察网
  • 标志设计ppt北京seo产品
  • 网站建设太金手指六六二八常州网站推广
  • 二手书哪个网站做的好360搜索引擎的特点
  • 如何组建网站网络营销成功的案例分析
  • 头条小程序seo服务收费
  • 个人简历模板在线编辑资源优化网站排名
  • 武汉光谷建设投资有限公司门户网站广州seo教程
  • 网站制作怎么赚钱百度网盘网站入口
  • node做网站优势2022年seo还值得做吗
  • 个人业务网站建设小姐关键词代发排名
  • 30天网站建设实录视频2022社会热点事件及看法
  • 旅游网站建设需求说明书网络营销策划公司
  • 静态网页设计素材seo在线优化工具 si
  • 天长做网站百度广告联盟平台官网
  • 上海做网站开发的公司湖南seo优化报价
  • 做企业网站 需要注意的百度指数的主要功能有
  • 如何做企业套模网站在线搭建网站
  • 惠州网站制作定制html制作网页代码
  • 河北邢台旅游景点推荐手机百度关键词优化
  • 贵金属如何用网站开发客户建站之星官网
  • 诸城市网站建设磁力bt种子搜索神器
  • 行政机关网站建设视频广告联盟平台
  • 接做网站单子的网站关键词怎么做快速的有排名