gpt4 book ai didi

react-native - JSON 解析错误 : Unrecognized token '<' - React Native

转载 作者:行者123 更新时间:2023-12-04 10:50:06 26 4
gpt4 key购买 nike

   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();
}

使用 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();

}

关于react-native - JSON 解析错误 : Unrecognized token '<' - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com