react-native - JSON Parse error: Unrecognized token '<' - React Native
问题描述
signIn = () => {
//post data to express backend
fetch('http://......./api/v1/auth', {
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: `login=${this.state.login}&password=${this.state.password}`
})
.then((response) => response.json())
.then((res) => {
if (res.success === true) {
this.props.navigation.navigate('Authorized')
} else {
alert(res)
}
})
.done();
}
I am continuously receiving such error and also network failed error when using localhost:3000
解决方案
添加try
catch
. 似乎响应是text/html
格式的。
signIn = () => {
//post data to express backend
fetch('http://......./api/v1/auth', {
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: `login=${this.state.login}&password=${this.state.password}`
})
.then(async (response) => {
let res;
try {
res = await response.clone().json();
} catch (e) {
res = await response.text();
}
return res;
})
.then((res) => {
if (res.success === true) {
this.props.navigation.navigate('Authorized')
} else {
alert(res)
}
})
.done();
}
推荐阅读
- python - 有没有办法强制 ruamel 在换行符之前的 OrderedDict 中插入一个新的(键:值)对?
- php - laravel 上的多重身份验证
- angular - Angular:有没有一种方法可以从 ts 文件访问表单中的所有表单控件,而无需显式地传递表单 html?
- c# - 如何在 EF Core 3.14 中运行存储过程
- javascript - 表单弹出功能未定义
- python - 更改列表中列表中的元素会更改整个列-python
- javascript - ComponentDidUpdate() 不断被调用会减慢进程(Axios)
- r - 使用正则表达式,在 r 中找到匹配项后如何添加元素?
- sequelize.js - 无法读取 model.update 方法中未定义的属性“选项”
- python - dvwa 暴力破解的 Python 循环