javascript - 无法在反应中发出 Post 请求
问题描述
我正在尝试在响应中进行 POST,并且我有一个错误说{"error_description":"Missing grant type"}
邮递员工作正常,我做错了什么?谢谢!
这是我的代码
class App extends Component {
constructor() {
super()
this.state = {
info : null
}
}
componentDidMount() {
var payload = {
client_id: 'my_site',
grant_type: 'my_credentials',
client_secret: 'xxx',
}
var data = new FormData();
data.append("json",JSON.stringify(payload));
fetch('/myendpoint', {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: data
})
})
.then(function(res){
return res.json();
})
.then(function(data){
alert( JSON.stringify( data ) )
})
解决方案
当您发出发布请求时定义类型:URL 编码但您发送 JSON
尝试使用以下格式发出请求:
fetch('/myendpoint?client_id="some id"&grant_type="some type"', {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
})
因为您在 URL 上的值使帖子没有正文
推荐阅读
- java - 安卓电报客户端:如何获取最后一条消息?
- javascript - 基于对象数组验证相同的值
- html - 右侧页面底部的空白区域,左侧正确填充背景色
- apache-spark - 数据类型转换火花数据框列 - pyspark
- r - R - 使用向量作为函数的默认参数列表
- c - 字符串链表的插入排序
- java - 如何修改 REST API 的 Spring 存储库搜索的页面类型结果?
- android-studio - 如果我已经拥有 Intellij IDEA Ultimate,是否需要安装 Android Studio?
- c# - 使用 Razor 视图在 Asp.net Mvc 中动态添加和编辑数据
- button - 如何定位按钮内图标的不透明度(不影响整个按钮)?