语法错误:JSON 解析错误:无法识别的令牌"<"本机反应



我们想添加一个事件"onPress";但该功能不起作用。后端正在工作(与邮递员一起(,但我们在获取捆绑包时出现了404错误:[SyntaxError:JSON分析错误:无法识别的令牌'<']

代码:

renderMesClubs = () => {
return this.state.sport.clubs.map((element) => {
return (
<TouchableOpacity
onPress={() => {
const data = {choixClub: JSON.stringify(element.nomClub)};
const headers = new Headers({
'Content-type': 'application/json',
});
const options = {
method: 'POST',
body: data,
headers: headers,
};
fetch('http://localhost:8080/inscription/choixclub', options)
.then((response) => {
return response.json();
})
.then(
(responseData) => {
this.setState({message: responseData.message});
},
(err) => {
console.log(err);
},
);
}}>
<View
style={{
justifyContent: 'center',
backgroundColor: 'rgba(209,209,209, .4)',
borderRadius: 20,
margin: 20,
}}>
<Text
style={{
textAlign: 'center',
marginTop: 30,
fontSize: 18,
backgroundColor: 'rgba (209,209,209, .4)',
}}>
{element.nomClub}
</Text>
<Image
style={styles.tinyLogo}
source={{uri: 'http://localhost:8080/' + element.logo}}
/>
</View>
</TouchableOpacity>
);
});
};

非常感谢您的宝贵帮助!!!

后端没有在404上返回有效的JSON。
您需要处理这种情况:

fetch("http://localhost:8080/inscription/choixclub", options)
.then((response) => {
if(response.status === 404) throw new Error('Resource not found');
return response.json();
})
.then((responseData) => {
this.setState({ message: responseData.message });
}
)
.catch((error) => {
console.error(error);
// TODO: manage not found error
});

也可以尝试这样传递标题:

headers: { 'Content-Type': 'application/json' }

在发送非对象数据时出现此错误。我本来应该发送{email: xyz@gmail.com},但我发送的是xyz@gmail.com

相关内容

最新更新