【问题标题】:POST data using Ajax, jquery, Node.js and Express使用 Ajax、jquery、Node.js 和 Express 发布数据
【发布时间】:2016-09-13 13:08:16
【问题描述】:

我需要一些帮助。我正在尝试按照我在互联网上发现的不同事情来完成这项工作,但我无法理解这个过程。 我有一个轮播,我的用户在注册后立即被重定向到。 我想通过询问简单的问题作为输入来获取有关他们个人资料的信息。 这是哈巴狗代码

body
div(class='container fill')
  div(id='myCarousel', class='carousel slide', data-ride='carousel', data-interval="false", data-wrap="false")
    div(class='carousel-inner')
      div(class='item active')
        div(class='carousel-caption')
          div(class='form-group', method='post')
            h1(id='welcome') WELCOME 
            h1
            | Tell us more about yourself
          h2
            | Pick any interest you like
          label(for='shopping', class="radio-inline")
            | Shopping
          input(type='checkbox', id='round', name='interest_filter', value='2', checked=interest=='shopping')

          div
          label(for='travelling', class="radio-inline")
            | Travelling
          input(type='checkbox', id='round', name='interest_filter', value='3', checked=interest=='travelling')

          div
          label(for='musique', class="radio-inline")
            | Musique
          input(type='checkbox', id='round', name='interest_filter', value='4', checked=interest=='musique')

          div
          label(for='cooking', class="radio-inline")
            | Cooking
          input(type='checkbox', id='round', name='interest_filter', value='5', checked=interest=='cooking')

          div
          label(for='nature', class="radio-inline")
            | Nature
          input(type='checkbox', id='round', name='interest_filter', value='6', checked=interest=='nature')

          div
          label(for='arts', class="radio-inline")
            | Arts
          input(type='checkbox', id='round', name='interest_filter', value='7', checked=interest=='arts')

这就是我不知道如何提供数据的地方

$('#matchme').click(function(){
var data = {};

$.ajax({
       url: '/carousel',
       type: 'POST',
       cache: false,
       data: JSON.stringify(data),
       success: function(data){
          alert('post success')
          console.log(JSON.stringify(data));
        }
      });
 });

这是在我的 app.js 中(我将使用瀑布来思考数据并将其存储在我的数据库中)

app.post('/carousel', function (req, res) {
var data = {
interests: req.body.interest_filter,
orientation: req.body.orientation_filter,
age: req.body.age
 },
});

我使用输入是因为您不能将表单放入轮播中。 到目前为止,我理解了这个概念,但是我已经使用 Javascript 一个月了,我被卡住了,请帮忙吗? 提前致谢!

【问题讨论】:

  • 究竟是什么问题,你卡在哪里? req.body 的值是什么?
  • 我被 ajax 部分卡住了,我不知道我应该怎么做,我怎样才能得到我的用户在数组中作为数据打勾的所有值,然后用 app.post 得到它? req.body 的值是用户根据兴趣勾选的复选框,例如(烹饪、旅行等)
  • 使用表单并序列化该表单
  • 我不能在轮播中使用表单,我只能使用输入。我只想获取我的用户检查的值

标签: javascript jquery ajax node.js express


【解决方案1】:

首先,您必须从用户那里收集所有信息并在 Ajax 调用中传递该对象。除了空白对象外,我看不到您在请求期间传递的任何数据。在服务器端之后,您可以在req.body 中获取您已经编写的所有信息..

     $('#matchme').click(function(){
         var data = {name:'hola', info: 'info'};

      $.ajax({
        url: '/carousel',
        type: 'POST',
        cache: false,
       data: JSON.stringify(data),
        success: function(data){
           alert('post success')
           console.log(JSON.stringify(data));
        }
       });
   });

【讨论】:

    【解决方案2】:

    首先,如果我正确理解问题,我建议使用 class 属性而不是 id 将“round”样式应用于每个元素。

    如果你改变它,你可以使用原生 JavaScript:document.getElementById('someId').value 语法来提取不同的值?

    如果你使用的是 JQuery 库,那就更简单了:$( "#someId" ).val(); http://api.jquery.com/val/

    【讨论】:

    • 您好,感谢您的回复,我也尝试过这种方式,但值仍然未定义,我不明白为什么。
    【解决方案3】:

    感谢您的帮助,我现在明白了这个概念。 到目前为止,它一直在处理这个问题:

    $(document).ready(function() {
    console.log("document is ready");
    $("#matchme").click(function(){
    event.preventDefault();
    var data = {};
    data.sex = $('[name=sex]:checked').val();
    data.orientation_filter = $('[name=orientation_filter]:checked').val();
    data.age = $('[name=age]:checked').val();
    
    console.log(data.sex);
    console.log(data.orientation);
    console.log(data.age);
    $.ajax({
             url: '/carousel',
             type: 'POST',
             cache: false,
             data: JSON.stringify(data),
             success: function(data){
                alert("post success")
                console.log(JSON.stringify(data));
              }
            });
       });
    
    });
    

    所以当我记录它时我可以得到 data.sex 的值,但是另外两个 data.orientation 和 data.age 仍然未定义。

    这是我的 app.js 文件:

    app.post('/carousel', function (req, res) {
    username = session.uniqueID;
    var data = {
    sex: req.body.sex,
    orientation: req.body.orientation_filter,
    age: req.body.age
    };
    console.log(data.sex);
    console.log(data.age);
    function UserStart(data, callback) {
    async.waterfall([
      function(cb) {
        pool.getConnection(function (err, connection) {
          if (err) {
            console.log(err);
          }
          connection.query('INSERT INTO usersinfo SET ?', data , function (err, rows, fields) {
            if (err) {
              console.log(err, "error")
            } else {
              callback(null, "all good")
            }
          });
        });
      }
    ],
    function (err, data) {
      if (err) {
        callback(err);
      } else {
        callback(null, data);
      }
    })
    }
    UserStart(data, function (err, callback) {
     if (err) {
       res.redirect('/login');
       console.log("login again");
     } else {
       res.redirect('/home');
       console.log("user is now activated, ready !");
     }
    })
    });
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 2011-06-09
      • 2023-04-02
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多