我正在尝试制作一个使用网站php登录到网站(我的学校日程安排站点)的应用程序。我收到了this错误/语法,有人可以帮我吗?我在下面添加了我的代码。
constructor(props){
super(props)
this.state={
userEmail:'',
userPassword:'',
}
}
login = () => {
const {userEmail,userPassword} = this.state;
alert(userEmail);
fetch('http://tref.cals.nl/roosters/infoweb/index.php',{
method:'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json',
},
body:JSON.stringify({
user: userEmail,
paswoord: userPassword,
})
})
.then((response) => response.json())
.then((responseJson)=>{
alert(responseJson);
})
Keyboard.dismiss();
}
}
最佳答案
您正在尝试发送和接收JSON数据-但提到的网页使用普通的HTML表单并使用HTML(而不是JSON)进行响应。
尝试类似:
let formdata = new FormData();
formdata.append("username", 'test')
formdata.append("paswoord", 'pw')
fetch('http://tref.cals.nl/roosters/infoweb/index.php', {
method: 'post',
body: formdata
})
.then((response) => response.text())
.then((response) => {
console.log(response); // prints the returned HTML
})
.catch((err) => {
console.log(err);
})
顺便说一句:发生的错误是因为您试图解析JSON字符串而失败的,因为它是HTML(以提到的
<
token 开头)。
关于php - react 本地错误,登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48309141/