Nodejs / Express Ajax POST调用


                    

我正在尝试从nodejs / express应用程序中的javascript函数进行Ajax调用。 功能代码如下:</ p>

  function Save_User_Changes(user_id){
     警报(“保存更改”)
     让数据= {}
     data.first_name = document.getElementById('first_name')。value;
     data.last_name = document.getElementById('last_name')。value;
     data.nickname = document.getElementById('昵称').value;
     data.email = document.getElementById('email')。value;
         $ .ajax({
             输入:“ POST”,
             数据:JSON.stringify(data),
             contentType:“ application / json”,
             网址:“ / users / save_user”,
             成功:功能(数据){
                 console.log('成功');
                 console.log(JSON.stringify(data));
             }
         });
}
</ code> </ pre>

这是我的路线文件:</ p>

  router.post('/ save_user',(req,res)=> {
//让obj = {};
console.log('body:'+ JSON.stringify(req.body));
返回res.send(req.body);
}
</ code> </ pre>

我确实收到了成功消息,并按预期方式打印了数据。 但是,/ users / save_users路由上没有任何反应。
预先感谢您的指导。</ p>
     </ div>

展开原文

原文

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个回答


您永远不会在POST呼叫服务器端进行应答。</ p>

您可以执行以下操作:</ p>

  router.post('/ save_user',(req,res)=> {
    console.log('body:'+ JSON.stringify(req.body));
    //这里可以进行处理以保存您的用户...
    返回res.sendStatus(201);
}
</ code> </ pre>
     </ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐