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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献