Javascript 收到此错误时的反应:";[未经处理的承诺拒绝:SyntaxError:JSON解析错误:意外标识符“Employee”;
我正在尝试对json格式进行字符串化,以便在json的post请求体中发送它。我得到一个错误,说:Javascript 收到此错误时的反应:";[未经处理的承诺拒绝:SyntaxError:JSON解析错误:意外标识符“Employee”;,javascript,node.js,json,react-native,Javascript,Node.js,Json,React Native,我正在尝试对json格式进行字符串化,以便在json的post请求体中发送它。我得到一个错误,说: [Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier "Employee"] * [native code]:null in parse - node_modules\promise\setimmediate\core.js:37:14 in tryCallOne -
[Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier "Employee"]
* [native code]:null in parse
- node_modules\promise\setimmediate\core.js:37:14 in tryCallOne
- node_modules\promise\setimmediate\core.js:123:25 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 in callImmediates
* [native code]:null in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:142:17 in __guard$argument_0
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue
下面是我的代码:
const CreateEmployee = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
const [salary, setSalary] = useState("");
const [picture, setPicture] = useState("");
const [position, setPosition] = useState("");
const [modal, setModal] = useState(false);
let body = JSON.stringify({
name: name,
email: email,
phone: phone,
salary: salary,
picture: picture,
position: position,
});
const submitData = () => {
fetch("http://94413b3f3960.ngrok.io/send-data", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: body,
}).then((res) => res.json());
// .then((data) => console.log(data));
};
我尝试将JSON字符串化后打印出来,结果打印正确,如下所示:
{"name":"Oma","email":"Dj","phone":"Fey","salary":"Jdd","picture":"","position":"Jjx"}
文本值是由一些文本输入测试的随机输入听起来像服务器不返回JSON?听起来像服务器不返回JSON?