【问题标题】:Cannot get Ajax variable in Node JS无法在 Node JS 中获取 Ajax 变量
【发布时间】:2021-01-08 04:21:17
【问题描述】:

我创建一个 AJAX 如下:

<script>
    $("button[type='submit']").click(function (){
       
        var length = $('.service__title').length;
        var title = [];
        var quantity = [];
        var subtotal = [];

        for(let i = 0; i < length; i++){

                title.push($('.service__title').eq(i).text());
                quantity.push(parseInt($('.display__number').eq(i).text()));
                subtotal.push(parseInt($('.display__total').eq(i).text()));

        }
        $.ajax({
            url: "../booking/success",
            type: "POST",
            data: {title:title, quantity:quantity, subtotal:subtotal}
            // contentType: "application/x-www-form-urlencoded"
          });
    });

</script>



当我在终端控制台登录 req.body 时,我得到:

{
  title: [
    '1 x Private Self Makeup Class',
    'Bridesmaid Look + Hair/Hijab do (no retouch)'
  ],
  quantity: [ '6', '3' ],
  subtotal: [ '4200000', '900000' ]
}
{
  Dkhang: 'Viasss',
  DPhone: '0943064201',
  DEmail: 'q12@gmail.com',
  DDate: '2020-09-16',
  DTime: '00:00',
  DAdress: 'Holo',
  DTinh: '11',
  DQuan: '11',
  service: '32',
  DComment: ''
}

请指导我如何在服务器的 index.js 中使用 Ajax。我不能跑

app.post("/", function(req,res){
    let x = req.body.title.join(", ");
    sql_text="SELECT * FROM Service WHERE Title IN ("+ x +")";
    db.query(sql_text, (err, rows)=>{
        if(err) res.send(err)
        else{
            res.render("services")
        }
    })
})

当我控制台记录 req.body.title 时,我得到 2 个对象:标题和未定义对象 请帮帮我!!!

【问题讨论】:

    标签: html css node.js sql-server ajax


    【解决方案1】:

    不确定您在控制台记录 req.body.title 时获得对象标题是什么意思。

    但你要求

    SELECT * FROM Service WHERE Title IN (1 x Private Self Makeup Class, Bridesmaid Look + Hair/Hijab do (no retouch))
    

    应该是

    SELECT * FROM Service WHERE Title IN ("1 x Private Self Makeup Class", "Bridesmaid Look + Hair/Hijab do (no retouch)")
    

    通过字符串 concat 查询 SQL 是很危险的 SQL injection。 查看 W3School 并了解如何使用占位符 ? 转义查询值

    或者我个人建议你改用knexsequelize

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      相关资源
      最近更新 更多