最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

每次运行此登录页面时都会刷新,而不是移至主页

网站源码admin21浏览0评论

每次运行此登录页面时都会刷新,而不是移至主页

每次运行此登录页面时都会刷新,而不是移至主页

这是代码的前端,在这里我尝试使用电子邮件和密码进入基本的登录页面

 onSubmit=(event)=>{
        console.log(this.state);
        fetch('http://localhost:3000/Signin',{
            method:'post',
            // headers:{'Content-Type': 'application/json'},
            "Content-type": "application/json; charset=UTF-8",
            body:JSON.stringify({
                email:this.state.signInEmail,
                password:this.state.signInPassword
            })
        }).then(response=>response.json())
          .then(data=>{
            if(data==='successs'){
                this.props.onRouteChange('home');
                console.log('done');
            }else{
                console.log('something went wrong');
            }
          });
          //this.props.onRouteChange('home');

        }

发布请求的后端

app.post('/Signin', (req, res)=>{
    if(req.body.email===database.users[0].email
        &&req.body.password===database.users[0].password)
    {
        console.log(req.body);
        res.json('successs');
    }else{
        res.json('failed');
    }
})

[每次我输入ID和密码时,它都会移动到主页一秒钟,然后返回登录和事件,尽管它是一个发帖请求,但我仍会像在获取请求的情况下一样显示电子邮件和密码

回答如下:

我认为您在提交功能中缺少preventDefault()

仅在获取调用之前,添加event.preventDefault()

发布评论

评论列表(0)

  1. 暂无评论