Warm tip: This article is reproduced from serverfault.com, please click

javascript-Nuxt.js在使用this时没有传递身体参数。

(javascript - Nuxt.js not passing body params when using this.$http.post)

发布于 2020-11-19 13:14:07

我遇到这个问题,当我使用@ nuxt / http提供的this。$ http.post和this。$ http.patch方法时,会导致发布时解析正文参数时出现问题。奇怪的是,它曾经可以工作,所以我什至不知道从哪里开始。

谁能指导我到哪里开始寻找解决方案?

谢谢

这是我的客户代码

await this.$http.patch("http://localhost:3000/api/tasks/${task.id}",{task:"rando info here"})

在我的app.js express服务器中,它看起来像这样

    app.use(express.json({strict:false}));
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use('/tasks', taskRouter)

并在我的route / tasks.js中

    router.patch('/:taskId', function(req,res,next){
        console.log(req.body)
        #a bunch of sql related code
    })

以便我可以看到发生了什么。

Questioner
T. H
Viewed
11
T. H 2020-11-28 11:17:19

不用担心..我知道了。这与我使用@ nuxt / http模块有关。由于某种原因,this。$ http.post('apilink:3000',{body:bodyinfo})不发送正文。它一定是一个错误。

对我有用的是更改为axios。

this.$axios.post('apilink:3000', {body:bodyinfo})

无论出于何种原因通过身体,而不是通过另一个。