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

网站建设涉及的法律易名中国域名门户网站

网站建设涉及的法律,易名中国域名门户网站,百度关键词竞价,深圳市住房和建设局门户网站在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/a/541148.html

相关文章:

  • 企业网站托管的方案软件开发流程详细
  • 做推广适合哪些网站深圳办公室设计公司排名
  • 做网站设计怎么提升自己怎么搭建个人博客网站
  • 测试题-4
  • 莱西大型网站建设做宣传海报的网站
  • Coze套餐实现工作总结
  • 做新网站的swot分析怎样选择网站建设
  • Mantle Global Hackathon 2025:里程碑升级后的首场生态猎星行动!
  • 景观建设网站宁波网站推广多少钱一个
  • Spring JDBC高级操作全解析
  • Matlab混合编程技术学习教程——目录
  • 基于MATLAB的LBFGS优化算法实现
  • 【matlab】字符串数组 转 double
  • 技术速递|Playwright MCP 调试 Web 应用时,GitHub Copilot 生成断言脚本的实用方法
  • RTSP低延迟播放重构:SmartMediaKit如何让系统“看见即行动”
  • 技术文档搭建实战:基于PandaWiki的五步自动化方案
  • wordpress能做手机站么电商网站设计系列
  • 深入剖析SLAB分配器原理与优化实战
  • 建设安全备案登入那个网站wordpress文章微信公众号推送
  • 6.1.3.1 大数据方法论与实践指南-开源大数据离线调度平台
  • 技术支持 东莞网站建设石材seo智能优化系统
  • 南沙区建设局网站如何进行网站域名解析
  • GNSS+LiDAR+Camera(双目)+IMU(战术级)的多传感器融合定位-WayFinder
  • HTML基础(一)
  • 诺亚财富汪静波:在波动中捕捉结构性机会,创造穿越周期的长期价值
  • SAP SD销售开票及回款信息查询接口分享
  • 更新维护:定期更新、功能修复、性能优化的全面指南
  • vue3 全局定义动态样式
  • 常州网站建设效果黑马程序员学费多少钱
  • 昆山设计网站的公司深圳市涂能装饰设计公司网站