当前位置:网站首页>[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 .
边栏推荐
- Luogu p1073 [noip2009 improvement group] optimal trade (layered diagram + shortest path)
- First experience Amazon Neptune, a fully managed map database
- 飞机引气系统的建模与故障仿真
- Electronics: Lesson 011 - experiment 10: transistor switches
- Force buckle 272 Closest binary search tree value II recursion
- Drawing of clock dial
- Luogu p3313 [sdoi2014] travel (tree chain + edge weight transfer point weight)
- 深度学习系列45:图像恢复综述
- Electronics: Lesson 010 - Experiment 8: relay oscillator
- TCP 加速小记
猜你喜欢
Sword finger offer (simple level)
Talk about the future of cloud native database
將數據導入到MATLAB
Stm32cubemx Learning (5) Input capture Experiment
自制坡道,可是真的很香
剑指offer刷题(简单等级)
电子学:第012课——实验 11:光和声
Force buckle 272 Closest binary search tree value II recursion
Self made ramp, but it really smells good
Socket problem record
随机推荐
TCP MIN_RTO 辩证考
The era of enterprise full cloud -- the future of cloud database
STM32CubeMX 学习(5)输入捕获实验
ffmpeg+SDL2实现音频播放
协议和服务的区别?
Ffmpeg+sdl2 for audio playback
Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
[supplementary question] 2021 Niuke summer multi school training camp 9-N
Stm32cubemx learning (5) input capture experiment
Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
【补题】2021牛客暑期多校训练营9-n
TCP的那点玩意儿
Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
Machine learning notes linear regression of time series
Cloud computing exam version 1 0
Drawing of clock dial
57. insert interval
STM32CubeMX 學習(5)輸入捕獲實驗
Anaconda based module installation and precautions
Debugging mipi-dsi screen based on stm32mp157