简体   繁体   中英

Error: Uncaught SyntaxError: Unexpected token < javascript

Been looking around to see the solutions for Unexpected token < but none of them helped me or maybe I wasn't able to spot it. Anyway, here is my code hope someone will be able to see what i'm missing. The console indicates the error is somewhere in the line : "var vec = JSON.parse(data);"

Thanks!

(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();
})();

Based on what you've provided, I would guess you're getting something other than JSON in your response (most likely HTML, given the reference to < ). I'd suggest debugging and taking a look at what is inside your data variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM