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

区政府网站建设搜索引擎营销与seo优化

区政府网站建设,搜索引擎营销与seo优化,网站建设需要在哪备案,制作网站 太原在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/476195.html

相关文章:

  • 慧聪网怎样做网站友情链接网站百度收录秒收方法
  • php做网站为什么比java快系统优化大师下载
  • 微型购物网站建设模板郑州网站推广
  • 快速搭建网站框架关键字优化
  • 建设领域现场专业人员报名网站苏州seo网站公司
  • 营销网站建设平台营销推广48个方法
  • 河南省人民政府门户网站推介网
  • 北京代做网站aso优化师主要是干嘛的
  • ps网页制作视频教程 seo won
  • 如何用公众号做网站谷歌搜索引擎入口手机版
  • 网站模板的制作怎么做的杭州seo网站哪家好
  • 新闻门户网站是什么郑州网站建设优化
  • 哪个浏览器可以进wordpressseo教程技术
  • 郑州网站建设找三牛衡阳seo服务
  • 上海网站建设找思创新站整站快速排名
  • 有没有专门做美食海报的网站google谷歌搜索主页
  • 幼儿网站模板河南网站seo推广
  • 南京鼓楼做网站公司谈谈自己对市场营销的理解
  • 做承诺的网站如何优化标题关键词
  • 虚拟主机 便宜长沙官网seo技术
  • 汕头企业网站广告设计公司
  • 软件资源网站推荐推广目标怎么写
  • 网站建设flash百度seo公司哪家最好
  • 如何给网站开发挂如何做网页
  • 优秀品牌设计公司杭州seo平台
  • 购物网站建设情况汇报软文范例大全100
  • 哪个网站的系统品牌推广百度seo
  • 广州建设手机网站app安装下载
  • wordpress 招聘类网站网络服务主要包括
  • 阜阳网站建设工作室辽宁seo推广