为什么我的POST请求创建URL查询字符串?

时间:2020-05-23 07:42:59

标签: javascript reactjs http url post

我有一个有路由问题的React程序。我正在为注册系统使用POST请求,每当我点击注册时,都会输入用户输入的密码和电子邮件地址,并将其放在不需要的URL的顶部。我不知道为什么此POST请求在URL中显示此类内容。这也只是把我带到空白页。我认为这是.get('/');

的问题

这是我单击“注册”时调用的代码:

onSubmitSignIn = () => {
        fetch('http://localhost:3000/register', {
            method: 'post',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                email: this.state.email,
                password: this.state.password,
                name: this.state.name
            })
        })
        .then(response => response.json())
        .then(user => {
            if (user.id) {
                this.props.loadUser(user);
                this.props.onRouteChange('home');
            }
        })
    }

这是服务器上的所有路由:

app.get('/', (req, res) => { res.send() });
app.post('/signin', Signin.handleSignin(db, bcrypt));
app.post('/register', (req, res) => { Register.handleRegister(req, res, db, bcrypt) });
app.get('/profile/:id', (req, res) => { Profile.handleProfileGet(req, res, db)});
app.put('/image', (req, res) => { Image.handleImage(req, res, db)});
app.post('/imageurl', (req, res) => { Image.handleApiCall(req, res)});

有人知道为什么要创建URL参数吗?我没有告诉。 / signin路由也是POST请求,但它不执行该操作。

1 个答案:

答案 0 :(得分:1)

我会检查您的浏览器控制台,以确认它实际上是作为POST而不是GET发送的。 URL中包含数据表示它正在通过表单提交作为GET提交。我并不认为这有错,但是我会将您的方法从post更改为POST。我很可能怀疑您的表单标记可能设置为GET,并且您的自定义提取逻辑未正确取消表单提交事件。