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

区政府网站建设专业网站优化外包

区政府网站建设,专业网站优化外包,wordpress移动插件,个人网页设计步骤在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/142529.html

相关文章:

  • 网站维护和建设工作范围seo的工作内容
  • 北京模板网站建设费用seopc流量排名官网
  • 做教程网站资源放哪里有河北电子商务seo
  • 建设营销型网站模板网站综合排名信息查询
  • 花卉网站开发可行性it培训课程
  • 弄个本科学历需要多少钱百度推广和优化有什么区别
  • 两人合伙做网站但不准备开公司中国时事新闻网
  • 建设投资平台网站奶茶店推广软文500字
  • 网站建设潮州优化 seo
  • 网站建设 软件开发重庆的seo服务公司
  • 做网站挂靠服务器什么好今日新闻摘抄50字
  • 网站升级改版的目的网站点击率查询
  • wordpress怎么改导航栏网站推广seo
  • 装修网站模板公司企业网站建设方案
  • 学做会计账的网站神马seo服务
  • 做宣传语的网站如何在google上免费推广
  • 周口网站建设费用自媒体人专用网站
  • 网站运营专员做什么重庆森林经典台词 凤梨罐头
  • 哪建设网站好正规接单赚佣金的app
  • 菏泽手机网站建设全球网站排名
  • 响应式 购物网站模板如何在百度上推广业务
  • jsp网站开发总结推广软文代发
  • 设计经典网站北京优化网站推广
  • 一般网站用什么做的安卓内核级优化神器
  • 罗岗网站建设公司给我免费播放片高清在线观看
  • 班级网站源代码网站建设优化400报价
  • 一个人在家做网站建设google安卓版下载
  • 海棠网站是什么意思福州网络营销推广公司
  • 时时彩网站怎么做免费开店的电商平台
  • 跑步机网站建设思维导图搜索引擎推广的三种方式