javascript - SyntaxError:_callee3$ 处的 JSON 输入意外结束
问题描述
我正在尝试使用 REACTJS 将对象从前端传递到后端。
该对象看起来像这样:
{
"userID": "some-id",
"userName": "some-name",
"userDescription": "some desc",
"userType": "T",
"userPassword": "password",
"updatedBy": "admin",
"companyID": "0",
"passwordType": "P",
"passwordStatus": "S",
"userEnabledStatus": "E",
"userLockedStatus": "L",
"userStatus": "DFT",
"updatedOn": "2020-06-13"
}
我传递对象的 userAction 函数是:
export const insertUser = (user) => async (dispatch) => {
try {
const res = await fetch(
"/api/SecAdmin/Users/InsertUser",
{
method: "POST",
body: JSON.stringify(user),
headers: {
"Content-Type": "application/json",
},
}
);
const data = await res.json();
dispatch({
type: ADD_USER,
payload: data.Message,
});
} catch (err) {
console.log(err);
dispatch({
type: USERS_ERROR,
payload: err.response.statusText,
});
}
};
但我得到这个错误:
“SyntaxError:_callee3$ (userActions.js:77) 处的 JSON 输入意外结束”。
如果这是来自前端或后端的错误,也请告诉我。
这是 userActions.js 文件的完整代码。我在错误指向的行上添加了注释。
import axios from "axios";
import {
USERS_ERROR,
ADD_USER,
UPDATE_USER,
} from "./types";
// Add User (This is the line which is pointed by error in case of add
user)
export const insertUser = (user) => async (dispatch) => {
const config = {
headers: {
"Content-Type": "application/json",
},
};
try {
const res = await axios.post(
"/api/SecAdmin/Users/InsertUser",
user,
config
);
dispatch({
type: ADD_USER,
payload: res.data.Message,
});
} catch (err) {
console.log(err);
dispatch({
type: USERS_ERROR,
payload: err.response.statusText,
});
}
};
// Update User (This is the line which is pointed by error in case of
update user)
export const updateUser = (user) => async (dispatch) => {
const config = {
headers: {
"Content-Type": "application/json",
},
};
try {
const res = await axios.put(
"/api/SecAdmin/Users/UpdateUser",
user,
config
);
dispatch({
type: UPDATE_USER,
payload: res.data.Message,
});
} catch (err) {
console.log(err);
dispatch({
type: USERS_ERROR,
payload: err.response.statusText,
});
}
};
解决方案
推荐阅读
- c# - 绑定到位于行为类中的命令
- angular - 在父模型中添加 2 个模型
- firebase - 部署 Angular Universal 应用程序时找不到模块“firebase/app”
- java - Resilience4J 断路器配置无法正常工作
- javascript - 为什么变量的值在输出之前没有按预期更新?
- hibernate - 为什么hibernate在更新实体之前检索列表
- python - 使用 jinja2 和 Flask 创建一个表,该表将在第三列之后自动下降到新行
- html - 调用 C# 函数并将其绑定到 Razor 页面中的按钮,Umbraco
- c# - C# WebClient DownloadFile 只工作一次
- python-requests - 如何使用 Requests.py 进行多账户更改密码?