如何从nodejs和expressjs中的POST请求中提取URL参数

时间:2015-03-23 09:47:48

标签: ajax node.js post express

请求网址http://localhost:9090/rest-api/fetchDetailedProductInfo?prodId=1&tempId=123

fetchDetailedProductInfo: function (prodId) {                                      
    var self = this;
    var URL = 'http://localhost:9090/rest-api/fetchDetailedProductInfo'
    $.ajax({                                               
        url: URL,
        dataType: 'json',
        contentType:'json',
        type:'GET',
        data: {
            'prodId':prodId,
            'tempId':123
        },                       
        success:function(responce) {
            self.renderUI(responce.json);
        },
        error: function (err) {
            console.log('ERROR',err)
        }
    });                    
},

@ SERVER SIDE

router.get('/rest-api/fetchDetailedProductInfo', function (req, res) {  
    var prodId = req.param('prodId');
    var tempId = req.param('tempId');
    res.send(prodId + '----' + tempId);
}

1 个答案:

答案 0 :(得分:1)

我认为你与req.params和req.query相混淆。这是指向另一个question

的链接

使用req.query

 var prodId = req.query.prodId;
 var tempId = req.query.tempId;

see this