weixin_33738578 2018-11-04 18:34 采纳率: 0%
浏览 198

Nodejs / Express Ajax POST调用

I am trying to make an Ajax call from javascript function in nodejs/express application. The function code is below:

function Save_User_Changes(user_id) {
    alert('saving changes')
    let data = {}
    data.first_name = document.getElementById('first_name').value;
    data.last_name = document.getElementById('last_name').value;
    data.nickname = document.getElementById('nickname').value;
    data.email = document.getElementById('email').value;
        $.ajax({
            type: 'POST',
            data: JSON.stringify(data),
            contentType: 'application/json',
            url: '/users/save_user',
            success: function (data) {
                console.log('success');
                console.log(JSON.stringify(data));
            }
        });
}

And this is my routes file:

router.post('/save_user', (req, res) => {
// let obj = {};
console.log('body: ' + JSON.stringify(req.body));
return res.send(req.body);
}

I do get success message with data printed as expected. However, nothing is happening on the with the route /users/save_users. Thanks in advance for any guidance.

  • 写回答

1条回答 默认 最新

  • weixin_33712881 2018-11-04 18:43
    关注

    You never answer to your POST call server side.

    You could do as the following :

    router.post('/save_user', (req, res) => {
       console.log('body: ' + JSON.stringify(req.body));
       // Here could go the processing to save your user ...
       return res.sendStatus(201);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料