php - react 本地错误,登录

标签 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();
}

}

最佳答案

您正在尝试发送和接收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/

相关文章:

ubuntu - 缓慢的 Ubuntu SSH 登录

php - 在ajax()方法jQuery中未收到错误消息

java - JSP/Servlet 和 PHP 页面之间的通信?

php - 数组获取下一个/上一个元素

php - 动态依赖表: Pulling data from MySQL

ios - React native-iOS,“:CFBundleIdentifier”不存在

android - 在 docker 容器中运行 Android 模拟器

login - 在 Keycloak 禁用多重登录

php - ubuntu升级后xdebug不工作

react-native - 将 Expo Bare 转变为托管项目