首页 > 解决方案 > 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,
   });
 }

};

标签: javascriptreactjsreduxreact-redux

解决方案


推荐阅读