当前位置:网站首页>[unexpected token o in JSON at position 1 causes and solutions]
[unexpected token o in JSON at position 1 causes and solutions]
2022-06-25 08:11:00 【Xingyu 1969】
Unexpected token o in JSON at position 1 Causes of errors and solutions
Current end use JSON.parse When parsing the data returned by the server, it will report (Unexpected token o in JSON at position 1) error . The reason is that the returned data is an object , In use JSON.parse If you go to parse it, it will be redundant and an error will be reported . The solution can be solved by adding a judgment in the following way
Here are some
Inline code slice . var joinObj
var message;
var code;
var typeStr = typeof(result)
if (typeStr == 'object'){
message = result.message;
code = result.code;
} else if(typeStr == 'string'){
joinObj = JSON.parse(result)
message = joinObj.message
code = joinObj.code
}
if(code === 0) {
return window.alert(message);
}
else if(code === 1){
window.location.href = '/login.html'
return window.alert(message);
}
among result Is the data returned by the server , Use typeof Resolves the return value type , If it is object Type is not used JSON.parse(result) Otherwise use JSON.parse Generate an object . So you can get the attribute method .
边栏推荐
- TCP与UDP
- Mr. Tang's lecture on operational amplifier (Lecture 7) -- Application of operational amplifier
- Static web server
- Niuke: flight route (layered map + shortest path)
- 黑点==白点(MST)
- [deep learning lightweight backbone] 2022 edgevits CVPR
- 不怕百战失利,就怕灰心丧气
- STM32CubeMX 学习(5)输入捕获实验
- 共话云原生数据库的未来
- C examples of using colordialog to change text color and fontdialog to change text font
猜你喜欢

FM信号、调制信号和载波

Network model -- OSI model and tcp/ip model

Mr. Tang's lecture on operational amplifier (Lecture 7) -- Application of operational amplifier

Importer des données dans MATLAB

c#磁盘驱动器及文件夹还有文件类的操作

Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system

电子学:第009课——实验 7:研究继电器

电子学:第014课——实验 15:防入侵报警器(第一部分)
![[deep learning lightweight backbone] 2022 edgevits CVPR](/img/13/139d28621403020e3475a30f6148f8.png)
[deep learning lightweight backbone] 2022 edgevits CVPR

Static web server
随机推荐
Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
STM32CubeMX 学习(5)输入捕获实验
57. insert interval
How to create a new branch with SVN
五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)
Startup, shutdown and restart of Oracle and MySQL on Linux
Electronics: Lesson 012 - Experiment 11: light and sound
Pychart's wonderful setting: copy immediately after canceling the comment and bring #
Overview of image super score: the past and present life of image super score in a single screen (with core code)
企业全面云化的时代——云数据库的未来
【补题】2021牛客暑期多校训练营4-n
Functions should not specify operation types through variables
現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
Deep learning series 45: overview of image restoration
Self made ramp, but it really smells good
TCP 加速小记
使用pytorch搭建MobileNetV2并基于迁移学习训练
Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
电子学:第009课——实验 7:研究继电器
Importer des données dans MATLAB