【问题标题】:Passing data from AJAX to route in Node.js将数据从 AJAX 传递到 Node.js 中的路由
【发布时间】:2020-06-03 04:41:21
【问题描述】:

我不知道出了什么问题,但这是我的代码:

AJAX 文件:

$(function(){
    $('.markreviewed').on('click', function(e){
        e.preventDefault()
        var reviewid = $('.reviewid').val()
        $.ajax({
            url: '/a/'+reviewid,
            type: 'PUT',
            contentType: 'application/json',
            data: {
                vid: reviewid
            },
            success: function(res){
                console.log('done');
            }
        })
    })
})

路线文件:

rtr.put('/a/:vid', (req, res, next)=>{
    console.log(req.body)
  })

控制台输出:

`done`

终端输出:

{}

即使在使用 body-parser 中间件解析 JSON 数据之后。

没有任何帮助:passing variable from jQuery ajax to nodejs

【问题讨论】:

    标签: node.js ajax routes


    【解决方案1】:

    上网一查,终于在代码中遇到了问题。贴出上述问题的解决方案:

    $(function(){
        $('.markreviewed').on('click', function(e){
            e.preventDefault()
            var reviewid = $('.reviewid').val()
            $.ajax({
                url: '/a/'+reviewid,
                type: 'PUT',
                contentType: 'application/json',
                dataType: 'json',  // to parse data in json format
                data: JSON.stringify({vid: reviewid}), // it is important to stringify the data 
                success: function(res){
                    console.log('done');
                }
            })
        })
    })
    

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2018-04-02
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多