反应原生错误,登录

时间:2018-01-17 20:04:27

标签: php react-native login syntax-error

我试图使用网站的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();
}

}

1 个答案:

答案 0 :(得分:1)

您正在尝试发送和接收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(以提及的<令牌开头)。