【问题标题】:Not receiving the JSON object on onClick未在 onClick 上接收 JSON 对象
【发布时间】:2017-08-23 06:35:29
【问题描述】:

我有非常小的用例。用户输入一本书的 ISBN 并显示具有该特定 ISBN 的书籍。但是,只有一半的功能在起作用。

由于某种原因,我无法在ejs 中收到包含该书的 JSON,因此我只能用一本书填充表格。

我在这里调用搜索功能,然后我用书填充表格

function searchISBN() {
        var input = $('#IdSearchISBN').val();
        $(document).ready(function () {
            $.get('/searchISBN/' + input, function (data, status) {
                var trJSON = '';
                $.each(data, function (i, item) {
                    trJSON += '<tbody><tr><td>' +
                        data[i].ISBN +
                        '</td><td>' +
                        data[i].BookTitle +
                        '</td><td>' +
                        data[i].BookAuthors +
                        '</td><td>'
                        + data[i].Publisher +
                        '</td><td>' +
                        data[i].Description +
                        '</td><td>' +
                        data[i].CallNumber +
                        '</td></tr></tbody>';
                });
                $('#booksTable').append(trJSON);
            });
        });
    }

这是执行搜索并返回书籍的函数

app.get('/searchISBN/:searchID', function (req, res) {
    console.log(req.params.searchID);
    if (req.params.searchID != "" && req.params.searchID) {
        BookTable.find({ISBN: req.params.searchID}, function (err, booksList) {
            if (err) {
                res.status(500);
                return console.error(err);
            }
            console.log("I have sent data");
            console.log(booksList);
            res.send(booksList);
        });
    }
});

我确实放了日志来查看搜索功能是否真的发送了这本书,是的。但是,我无法收到它 以下是我得到的日志。

9780730324218 I have sent data [ { _id: 58c7d0694172ab199c6de78e, ISBN: '9780730324218', BookTitle: 'The Barefoot Investor : The Only Money Guide You\'ll Ever Need', BookAuthors: 'Scott Pape', Publisher: 'John Wiley & Sons Australia Ltd', Description: 'This is the only money guide you\'ll ever need That\'s a bold claim, given there are already thousands of finance books on the shelves.', CallNumber: ' 0730324214', __v: 0 } ]

有什么建议吗?

【问题讨论】:

  • 在 $.get('/searchISBN/' + input, function (data, status) { 之后放一个 console.log(data) 看看你得到了什么。然后删除 $(document)。 ready(function () { 你不需要那个。它只是在 dom 准备好并且你在不需要的函数中调用它时执行代码。
  • @Taintedmedialtd 我做了建议的更改。但是,console.log(data) 没有给出任何显示
  • 艰难的。 GET 请求在浏览器控制台中没有返回任何内容?甚至没有错误?
  • 没什么。我什至试过alert
  • 我在下面的答案中重构了前端代码。看看有没有帮助:

标签: jquery json node.js ajax ejs


【解决方案1】:

我认为前端 JS 和 $(document).ready() 作为函数的一部分存在问题:

function searchISBN() {
    $.get('/searchISBN/' + $('#IdSearchISBN').val())
    .done(function(data){
        var trJSON = '';
        $.each(data, function (i, item) {
            trJSON += '<tbody><tr><td>';
            trJSON += [data[i].ISBN,data[i].BookTitle,data[i].BookAuthors,data[i].Publisher,data[i].Description,data[i].CallNumber].join('</td><td>');
            trJSON += '</td></tr></tbody>';
        });
        $('#booksTable').append(trJSON);
    })
    .fail(function(){
        console.log("Error");
    });
};

返回“错误”,因此 GET 请求失败。我认为,与其他答案一样,ObjectID 正在扰乱您的浏览器。在应用程序中更新它,它现在应该可以工作了:

app.get('/searchISBN/:searchID', function (req, res) {
    if (req.params.searchID != "" && req.params.searchID) {
        BookTable.find({ISBN: req.params.searchID}, function (err, booksList) {
            if (err) {
                res.status(500);
                return console.error(err);
            }
            var response = [];
            booksList.forEach(function(book) {
                var _book = book.toObject;
                delete _book._id;
                response.push(_book);
            })
            res.send(response);
        });
    }
});

【讨论】:

    【解决方案2】:

    您返回的数据不是有效的 JSON,因为 _id 属性是一个字符串,但没有正确引用。修复这个问题,代码就可以工作了:

    var data = [{
      _id: '58c7d0694172ab199c6de78e',
      ISBN: '9780730324218',
      BookTitle: 'The Barefoot Investor : The Only Money Guide You\'ll Ever Need',
      BookAuthors: 'Scott Pape',
      Publisher: 'John Wiley & Sons Australia Ltd',
      Description: 'This is the only money guide you\'ll ever need That\'s a bold claim, given there are already thousands of finance books on the shelves.',
      CallNumber: ' 0730324214',
      __v: 0
    }]
    
    var trJSON = '';
    $.each(data, function(i, item) {
      trJSON += '<tbody><tr><td>' +
        data[i].ISBN +
        '</td><td>' +
        data[i].BookTitle +
        '</td><td>' +
        data[i].BookAuthors +
        '</td><td>' + data[i].Publisher +
        '</td><td>' +
        data[i].Description +
        '</td><td>' +
        data[i].CallNumber +
        '</td></tr></tbody>';
    });
    $('#booksTable').append(trJSON);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="booksTable"></table>

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2014-08-27
      • 2019-08-18
      • 1970-01-01
      • 2022-01-18
      • 2018-12-29
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多