表达PUT空身

时间:2017-02-19 23:03:56

标签: node.js mongodb express

我正在使用MEAN Stack中构建的REST api进行更新请求。 在提交HTTP PUT请求时,我的'req.body'始终为空。

我已经探索过了,我在REST API服务器端使用了以下内容:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

从客户端,这是我的请求:

$http.put('http://localhost:8080/api/applications/'+data.name,
        {params:{'test' : 'testvalue'}},config)
        .success(function(data){
                    console.log(data);
        })

当我试图通过使用'req.body.test'来获得测试的价值时,我总是得到'未定义'并且进一步'req.body'是空的。

此外,客户端是角度的$ http(角度1.5.8),在服务器端,我在中间件中使用以下内容:

// middleware to use for all requests
router.use(function(req, res, next) {
    // do logging
    console.log('Something is happening.');
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    next(); // make sure we go to the next routes and don't stop here
});

,这是REST API中的端点:

router.route('/applications/:name')
.put(function(req, res) {
     console.log(req.params.name + "  " + req.body.test);
    res.json({ message: req.body.test});
                 // });
    })

请告知。

0 个答案:

没有答案
相关问题