首页 > 解决方案 > req.body 在快递的 307 重定向上获得两次

问题描述

我的表单有一个奇怪的问题,无法提交 un express js 应用程序。

当用户提交表单时,一些数据会POST发送到另一条路由并重定向到同一条路由。

看法

<form action="action" method="post">

<input type="submit" name="submit" value="submit">
</form>  

当用户提交action路由时触发

const action = function action(req,res){

    //make a post request 
    requestify.request('http://localhost:3005/idg/webhook', {
        method: 'POST',
        body: {
            foo: 'bar',
            bar: 'foo'
        },
        headers: {
            'Content-type': 'application/json'
        },
        dataType: 'json'
    })
    .then(function(response) {

    });
    //redirect to the same route
     res.redirect(307,'http://localhost:3005/idg/webhook');

}

这是idg/webhook路线

const webhook = function webhook(req,res){
    console.log("body",req.body);
    res.send(req.body);
}

问题是我req.bodyidg/webhook路上有两个。

console.log("body",req.body)idg/webhook

body { foo: 'bar', bar: 'foo' }
body { submit: 'submit' }

如您所见,我能够得到两个req.body为什么会这样?我只需要req.body ie{ foo: 'bar', bar: 'foo' }

标签: javascriptnode.jsexpressejshttp-redirect

解决方案


从输入提交中删除名称,更改如下:

<form action="action" method="post">
<button type="submit" >Submit</button>
</form>

推荐阅读