React native 提取不起作用
我正在等待来自服务器的JSON成功: {…“} 实际行为 我明白了 语法错误:JSON中位于位置0的意外标记b b是单词“badlogin”的第一个字母。当发送错误的用户名和密码组合时,它会响应服务器。但是,当我在同一地址上使用具有相同键值组合的邮递员时,我从服务器上获得了正确的回复 复制步骤React native 提取不起作用,react-native,React Native,我正在等待来自服务器的JSON成功: {…“} 实际行为 我明白了 语法错误:JSON中位于位置0的意外标记b b是单词“badlogin”的第一个字母。当发送错误的用户名和密码组合时,它会响应服务器。但是,当我在同一地址上使用具有相同键值组合的邮递员时,我从服务器上获得了正确的回复 复制步骤 fetch('http://....', { method: 'POST', headers: { 'Accept': 'application/json', 'C
fetch('http://....', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
userName: "react",
password: "123",
})
}).then((response) => response.json())
.then((responseJson) => {
console.log(responseJson.message);
if(responseJson.success === true) {
alert('successufuly loged');
}
else{
console.log(responseJson.message);
alert(responseJson.message);
}
})
}
}您正在尝试分析字符串。这就是错误所在。不要总是解析json,只需添加一个子句来检查请求是否成功
}).then((response) => {
if(!response.ok) {
// handle your error here and return to avoid to parse the string
return
}
return response.json()
})
.then()
您正在尝试分析字符串。这就是错误所在。不要总是解析json,只需添加一个子句来检查请求是否成功
}).then((response) => {
if(!response.ok) {
// handle your error here and return to avoid to parse the string
return
}
return response.json()
})
.then()
看起来您得到的响应不是
json
尝试检查您首先得到的响应:
.then((response) => response.text())
.then((responseJson) => {
console.log(responseJson);
}
看起来您得到的响应不是
json
尝试检查您首先得到的响应:
.then((response) => response.text())
.then((responseJson) => {
console.log(responseJson);
}
我通过使用FormData为发送准备数据来解决此问题:
......
login = () => {
var formData = new FormData();
formData.append('username', 'react');
formData.append('password', '123');
fetch('http://......', {
method: 'POST',
body: formData
........
我通过使用FormData为发送准备数据来解决此问题:
......
login = () => {
var formData = new FormData();
formData.append('username', 'react');
formData.append('password', '123');
fetch('http://......', {
method: 'POST',
body: formData
........
尝试删除
,“123”
之后的尾随,
。同时使用浏览器的Web Developer工具(F12)检查“网络”选项卡,检查对应用程序登录的请求,特别是其中的请求和响应部分。@Filburt没有帮助。@SaschaM78在“网络”选项卡上,我没有app loginTry删除尾随的,
在“123”
之后。同时使用浏览器的Web Developer Tools(F12)检查网络选项卡,检查对
应用程序登录的请求,尤其是请求和响应部分。@Filburt没有帮助。@SaschaM78在网络选项卡上我没有应用程序登录测试建议,但如果我发出警报(“连接错误”);在if(!response.ok){我没有收到警报。有趣的建议,但是如果我发出警报(!response.ok){我没有收到警报。谢谢。现在我清楚地看到了响应。它是“badlogin”。欢迎您,希望您的问题很快得到解决。谢谢。现在我清楚地看到了响应。它是“badlogin”.不客气,希望你的问题尽快得到解决。