1、首先js添加到桌面fetchAPI提供了一个JavaScript接口。
2、其次用于访问和操纵HTTP管道的部分。
3、最后只要浏览器支持js就能运行,规避了IE限制。
FormData是一个用于封装HTTP请求数据的对象,采用键值对的方式。// 实例化一个FormData对象
let formData = new FormData()
// 添加数据
formData.append('nameEn', 'xxxxx')
formData.append('giftId','xxxxx')
formData.append('csrfToken','xxxxx')
// 调用接口
fetch('接口地址', {
method: 'POST',
body: formData
})
.then(response =>response.json())
.catch(error =>console.error('Error:', error))
.then(response =>console.log('Success:', response))
增加credentials: 'include',参数即可带cookie发请求示例:
fetch('./api.php?action=getJson', {
method: 'POST',
credentials: 'include',
headers:{
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'test=test'
})