javascript - 如何使用 JS Fatch Api 发布 JSON 有效负载(名称、电子邮件)
问题描述
如何使用 JS Fatch api 函数发布 JSON 有效负载,此外,请求必须包含不记名(令牌)授权标头
解决方案
你好,尝试使用这个例子,它会有所帮助
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
因此,此代码将帮助您传递数据和所需的标头或参考https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
推荐阅读
- apache-spark - 无法在 HDInsight 4.0 中创建 Jupyter Notebook
- excel - 返回字符串匹配单元格内容的任何部分的范围内的第一行号
- tableau-api - Tableau 扩展,编写自定义扩展以跨多个工作表下载数据
- android - 智能手机上没有复选标记的 Android 复选框(模拟器正在工作)
- javascript - 使用 es6 类从继承 EventEmitter 的类中捕获事件
- parsing - 如何解析维基词典模板和模块
- java - 在 if else 中初始化变量但不工作,如何正确初始化它们?
- python - 在 BeautifulSoup Python 上找到 Botton 的问题
- typescript - 即使在修改命名空间后,Typescript 也无法编译
- c++ - c++ 动态指针数组 - 我什么时候需要使用它?