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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛