【问题标题】:Data not collected correctly from JSON object using JQUERY未使用 JQUERY 从 JSON 对象正确收集数据
【发布时间】:2015-12-19 17:33:03
【问题描述】:

我正在尝试创建一个从服务器端的数据库中选择数据并将其设置为客户端文本框的值的函数。它在服务器端工作正常,但在客户端,如果我为数据编写一个 console.log,它会显示undefined,我不知道为什么。非常感谢!

服务器端:

app.post('/id', function(req,res) {

    var data = req.body;
    var id = data.id;
    console.log(id);
    var query = "SELECT * FROM Control WHERE id=" +id;
    connection.query(query, function(error, result) {
            console.log(result);
            res.json(result);
    });
});

客户端:

function select()
            {
                var id = $('#nr_reg').val();
                $.ajax({
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    url: '/id',
                    data : {
                        id: id
                    },
                    success : function(data){           
                        console.log(data);                          
                        var id = data.id;
                        $('#optic').val(id);

                    },
                    error: function(err){
                        console.log(err);
                    }

                }); 
            }

查询后服务器的结果:

5 //this is the id which is correct
[ { id: 5,
    data: '',
    kw: '6',
    nr_articol: '279630-99',
    proiect: 'AUHE',
    optic: '0',
    electric: '',
    reelectric: '',
    smd: 'on',
    scurt_smd: '0',
    incomplete_smd: '',
    bile: '',
    val: '',
    scurt_val: '',
    incomplete_val: '',
    nivel_cositor: '',
    greseli_smd: '',
    lipsa_smd: '',
    invers_smd: '',
    plantare_manuala: '',
    componente_lipsa: '',
    componente_inversate_1: '',
    greseli_hotbar: '',
    lipire_hotbar: '',
    greseli_asamblare: '',
    componente_inversate_2: '',
    greseli_imprimare: '',
    componente_inversate_3: '',
    greseli_aspect: '',
    fire_lovite: '',
    componente_inversate_4: '',
    casaste: '',
    reparate: '',
    Total: '0' } ]

【问题讨论】:

  • 服务器端对象是什么样的,您收到的客户端对象又如何?你希望它是什么样子的?
  • 您在 jquery ajax 调用中有两个数据选项。数据:JSON.stringify(data) 和数据:{id: id}
  • 看起来有点像混合的 post/get 请求..
  • 我已经更新了第一个帖子,看看控制台如何显示查询后的结果。

标签: javascript jquery json node.js


【解决方案1】:

尝试使用data[0]

由于您的结果是:[{ id: 5,.... }],它表示数组中的一个对象。 为了 data.id 工作,你的结果应该是{id: 5,.....}。所以试试这个:

            function select()
            {
                var id = $('#nr_reg').val();
                $.ajax({
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    url: '/id',
                    data : {
                        id: id
                    },
                    success : function(data){           
                        console.log(data);                          
                        var id = data[0].id;
                        $('#optic').val(id);

                    },
                    error: function(err){
                        console.log(err);
                    }

                }); 
            }

【讨论】:

  • 不客气! [] 表示它在一个数组中,这就是我猜测数据 [0] 的原因
猜你喜欢
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 2018-02-12
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2011-10-30
相关资源
最近更新 更多