javascript - 如何修复“错误”;获取 api 中的 json 无效
问题描述
我正在使用 fetch api 将数据发布到托管在 heroku 上的 API。我收到错误消息 {error:Invalid JSON}
我该怎么办。
const signUpBtn2 = document.getElementById("enter2");
signUpBtn2.addEventListener("click", function(e){
e.preventDefault()
const formData = new FormData()
const fetchData = {
method: "POST",
headers: {
Accept: "application/json, text/plain, */*",
"Content-type": "application/json; charset=UTF-8"
},
body: formData
};
fetch("https://polityco.herokuapp.com/auth/signup", fetchData)
.then(function(res) {
return res.json();
})
.then(function(text) {
console.log([text]);
})
.catch(function(error) {
console.log(error);
});
});
我期望 json 响应,但我得到的响应是 [{…}] 0: {error: "Invalid JSON"} length: 1
解决方案
推荐阅读
- entity-framework - System.InvalidOperationException:关系特定的方法只能在上下文使用关系数据库提供程序时使用
- c# - 将 sbyte[] 转换为 bool[] 并将 char[] 转换为 short[]
- android - 如何映射从 ImageReader Camera2 接收的图像的 CameraResult 的 getFrameNumber
- python - 如何正确恢复张量流中的模型?
- java - 我无法从 GraphRequest().executeAsync() 中提取任何值
- forms - Spring Post Form @RequestBody 不能绑定为 MultiValueMap
- javascript - 为没有滚动功能的dom元素创建滚动条?
- ruby-on-rails - 每当基于模型的特定时间 gem cronjob
- android-ndk - 编译arm代码的严重clang错误
- r - 如何根据条件消除匹配模式