最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Uncaught TypeError: cannot read property 'length' of null" in jQuery - Stack Ove

programmeradmin1浏览0评论

I'm defining the following action to happen when pressing a button on my HTML:

$(document).ready(function() {
  $("#query").keydown(function () {
    // stuff
    $.get(url, function (result) {
    console.log(result);

    var list = "";

    for (var i = 0, l = result["results"].length; i < l; i++) {
      list += '<li>' + result["results"][i]["label"] + '</li>';
    }

    list = "Here are some results: <ul>" + list + "</ul>";

  });
});

What arrives in "result" is a JSON array in the following form:

{"results":[{"label":"something"},{"label":"something else"},{"label":"many other ones"}]}

So, why is my reference to length being interpreted as a reference to the property of a null value?

I'm defining the following action to happen when pressing a button on my HTML:

$(document).ready(function() {
  $("#query").keydown(function () {
    // stuff
    $.get(url, function (result) {
    console.log(result);

    var list = "";

    for (var i = 0, l = result["results"].length; i < l; i++) {
      list += '<li>' + result["results"][i]["label"] + '</li>';
    }

    list = "Here are some results: <ul>" + list + "</ul>";

  });
});

What arrives in "result" is a JSON array in the following form:

{"results":[{"label":"something"},{"label":"something else"},{"label":"many other ones"}]}

So, why is my reference to length being interpreted as a reference to the property of a null value?

Share Improve this question edited Sep 21, 2016 at 14:23 nyedidikeke 7,6188 gold badges47 silver badges61 bronze badges asked Dec 4, 2013 at 22:07 jl.lykonjl.lykon 3292 gold badges3 silver badges9 bronze badges 4
  • Try console.log(typeof result) instead, and tell us what it said. – adeneo Commented Dec 4, 2013 at 22:12
  • It's two lines: "object", followed by "undefined". – jl.lykon Commented Dec 4, 2013 at 22:18
  • Seems like you have to convert stackoverflow.com/questions/5533192/… – loveNoHate Commented Dec 4, 2013 at 22:20
  • 1 how can one console.log produce two lines? – adeneo Commented Dec 4, 2013 at 22:21
Add a comment  | 

1 Answer 1

Reset to default 13

I believe you are getting a json input. You forgot to convert the json into an actual Javascript Object. You can do so with one of the two following ways.

$(document).ready(function() {
  $("#query").keydown(function () {
  // stuff
    $.get(url, function (result) {
    result = JSON.parse(result);
    console.log(result);

    var list = "";

    for (var i = 0, l = result["results"].length; i < l; i++) {
      list += '<li>' + result["results"][i]["label"] + '</li>';
    }

  list = "Here are some results: <ul>" + list + "</ul>";

});

Or

$(document).ready(function() {
  $("#query").keydown(function () {
  // stuff
    $.getJSON(url, function (result) {
    console.log(result);

    var list = "";

    for (var i = 0, l = result["results"].length; i < l; i++) {
      list += '<li>' + result["results"][i]["label"] + '</li>';
    }

  list = "Here are some results: <ul>" + list + "</ul>";

});
发布评论

评论列表(0)

  1. 暂无评论