错误:未捕获的SyntaxError:意外的令牌< JavaScript的

时间:2014-08-25 14:35:48

标签: javascript json

一直在四处寻找意外令牌的解决方案<但他们都没有帮助我,也许我无法发现它。 无论如何,这是我的代码希望有人能够看到我所缺少的。 控制台指示错误在行中的某处:“var vec = JSON.parse(data);”

谢谢!

(function()
{
var xhr = new XMLHttpRequest();
xhr.open("GET","https://nodejs-c9-nathan14.c9.io",false);
xhr.onreadystatechange = function()
{
    if (xhr.readyState == 4 && xhr.status == 200)
    {
        var list = document.getElementById('list');
        var total_tasks = document.getElementById('total_tasks');
        var data = xhr.responseText;
        var vec = JSON.parse(data);
        vec.forEach(
          function(ob)
          {
              var li = document.createElement("li");
              var a = document.createElement("a");
              var div_head = document.createElement("div");
              var div_content = document.createElement("div");
              var delete_button = document.createElement("a");
              var date_image = document.createElement("img");

              div_head.className = "list_head";
              div_content.className = "list_content";

              // Get and set task name
              var task_name = document.createTextNode(ob.task);
              a.appendChild(task_name);
              a.setAttribute("href","edit.html?" + ob.id);

              // Set date image
              date_image.setAttribute("src","themes/images/sort_due_date.png");

              // Get date and fix to remove time
              var temp_task = ob.due_date;
              var temp_task_string = temp_task.toString();
              var temp_task_string_cut = temp_task_string.slice(0,10);
              var task_date = document.createTextNode(temp_task_string_cut);

              // Set delete button
              delete_button.setAttribute("data-role","button");
              delete_button.setAttribute("data-icon","delete");
              delete_button.setAttribute("data-iconpos","notext");
              delete_button.setAttribute("data-theme","b");
              delete_button.setAttribute("href","#delete_dialog?" + ob.id);
              delete_button.className = "ui-btn-right";

              // Append vars into DOM
              div_head.appendChild(a);
              div_content.appendChild(date_image);
              div_content.appendChild(task_date);
              div_content.appendChild(delete_button);
              li.appendChild(div_head);
              li.appendChild(div_content);
              list.appendChild(li);
          }
        );
        $('#list').listview('refresh');

   }
}
xhr.send();
})();

1 个答案:

答案 0 :(得分:0)

根据您提供的内容,我猜您在回复中会得到JSON以外的内容(最有可能是HTML,考虑到<的引用)。我建议调试并查看data变量中的内容。