【发布时间】:2020-04-14 15:53:33
【问题描述】:
我在向服务器发送数据时遇到问题。 当我尝试通过邮递员成功提交数据时,回复如下 如果我激活 '内容类型':'应用程序/json'
我有一个问题是 JSON 解析错误:无法识别的令牌 '
{
"status": 200,
"message": "success login",
"id_kurir": "3",
"username": "tester",
}
当我尝试使用代码时,我收到一个错误,即 [object object]
这是我的代码:
constructor(props) {
super(props);
this.state = {
Uname : '',
Upass : ''
}
}
login= ()=>{
const {Uname,Upass} = this.state;
fetch('https://example.com/login', {
method: 'POST',
// headers: {
// 'Accept' : 'application/json',
// 'Content-Type': 'application/json'
//},
body: JSON.stringify({
username: Uname,
password: Upass
})
})
.then((response) => response.json())
.then((responseJson) => {
alert(responseJson);
console.log(JSON.stringify(responseJson));
}).catch((error)=>{
console.log(error);
})
Keyboard.dismiss();
}
<Form style={styles.mainForm}>
<Item style={styles.formItems}>
<Input placeholder="Username" style={styles.Input} onChangeText={Uname => this.setState({Uname})}/>
</Item>
<Item style={styles.formItems}>
<Input style={styles.Input} secureTextEntry={true} onChangeText={(Upass) => this.setState({Upass})}/>
</Item>
<View style={styles.Button}>
{/* <Button block style={styles.mainBtn} onPress={() => this.props.navigation.navigate('home')}> */}
<Button block info style={styles.mainBtn} onPress={this.login}>
<Text style={styles.btnText}>Submit</Text>
</Button>
</View>
</Form>
错在哪里?
【问题讨论】:
-
试试 alert(JSON.stringify(responseJson));因为警报不能显示对象。
-
完成了,我已经正确填写了用户名和密码,但是为什么没有找到响应?即使我已经尝试使用 Postman 正确填写用户名和密码并且结果是合适的......
标签: javascript reactjs api react-native