【问题标题】:Why I always get "Uncaught SyntaxError: Unexpected token u " from Chrome?为什么我总是从 Chrome 收到“Uncaught SyntaxError: Unexpected token u”?
【发布时间】:2013-02-04 06:19:11
【问题描述】:

我花了一整天的时间在谷歌上搜索并寻找答案,但仍然无法弄清楚。

我的代码有点长,它在 Firefox 中运行良好,但从 Chrome 中得到“Uncaught SyntaxError: Unexpected token u”。

谁能指出我哪里错了?提前致谢!

// when the page loads, list all the current contacts
$(document).ready(function(){

    // check if localStorage database exists
    if(!localStorage.getItem("customerDatabase")){

        // define a JSON object to hold all current address
        var contacts = {
            "users":[
                {
                    "id":"1",
                    "name":"dennis",
                    "email":"dennisboys@gmail.com"
                },
                {
                    "id":"2",
                    "name":"zoe",
                    "email":"zoeisfemale@gmail.com"             
                }
            ]   
        } // end of contacts JSON object

        // stringify the object 
        var stringObject = JSON.stringify(contacts);                

        // store it into localStorage database
        var storedDatabase = localStorage.setItem("customerDatabase", stringObject);                                            

    } else {
        // list all customers upon page loads
        listJSONCustomers();        
    }   

    // list all current contacts from JSON object in localStorage
    function listJSONCustomers(){

      var displayHTML = "";
      var i;

      // get the data from localStorage
      var storedDatabase = localStorage.getItem("customerDatabase");

      // parse the data from string to JSON object
      var parseObject = JSON.parse(storedDatabase); 

      // access the users key of the JSON object
      var userObject = parseObject.users;

      // get the length of the object (how many customers the database has)
      var contactsLength = userObject.length;     

      for(i=0; i<contactsLength; i++){
          var trElement = '<tr id="address' + (i+1) + '">';
          var tdId = '<td id="id' + (i+1) + '">' + userObject[i].id + '</td>';
          var tdName = '<td id="name' + (i+1) + '">' + userObject[i].name + '</td>';
          var tdEmail = '<td id="email' + (i+1) + '">' + userObject[i].email + '</td>';
          var tdButton = '<td id="button"><button id="editButton' + userObject[i].id + '">Edit</button> | <button id="deleteButton' + userObject[i].id + '">Delete</button></td>';

          displayHTML += trElement + tdId + tdName + tdEmail + tdButton + '</tr>';
      }     

      $('#address_list').html(displayHTML);           
    }       

    // add customer to database  
    $('#saveCustomer').click(function(){

       if( $('#customerName').val() !== "" && $('#customerEmail').val() !== "" ){

           var customerName = $('#customerName').val();
           var customerEmail = $('#customerEmail').val();

           // get the data from localStorage
           var storedDatabase = localStorage.getItem("customerDatabase");

           // parse the data from string to JSON object
           var parseObject = JSON.parse(storedDatabase);    

           // access the users key of the JSON object
           var userObject = parseObject.users;     

           // get the new entry
           var newCustomerObject = {
                                  "id": userObject.length + 1,
                                  "name": customerName,
                                  "email": customerEmail
                                  };

           // push the new entry into the object                                                            
           userObject.push(newCustomerObject);

           // convert the object into string for localStorage
           var stringObject = JSON.stringify(parseObject);         

           // store the JSON object into localStorage
           var storedDatabase = localStorage.setItem("customerDatabase", stringObject);

           // list all customes again every time a database receives a new entry
           listJSONCustomers();     

       } else {
          alert("Please enter customer's name and email.");  
       }

    }); // end of $('#saveCustomer').click();


});

【问题讨论】:

  • 如果我复制并通过此代码,我不会在 chrome 中收到该错误。你确定错误属于这个文件,而不是例如扩展?
  • 也许您之前的本地存储中保存了一些损坏的内容?如果在调用parse() 之前打印字符串,它看起来是否正确?根据错误,localstorage 值在您的“用户”键周围没有引号。
  • @loganfsmyth,我在调用 parse() 之前打印了字符串,Firefox 返回一个不错的字符串,但 Chrome 返回“未定义”。但这是怎么回事?
  • @Dennisboys 这意味着您在某些时候不小心将 undefined 写入 localStorage。
  • @loganfsmyth 谢谢!我使用 localStorage.remove(),它也适用于 Chrome。 =D!!你拯救了我的一天!再次感谢!

标签: javascript json


【解决方案1】:

在某些时候,您所做的某事破坏了该键的 LocalStorage 的值。 LocalStorage 只能存储字符串,因此如果您向其传递任何其他内容,它会将其转换为字符串。由于您的值是'undefined',这意味着在某些时候,您可能会不小心做了这样的事情:

var value;
localStorage.setItem('key', value);

在这种情况下,valueundefined,它不是字符串。当它被保存时,它将被转换。不幸的是,"undefined" 不是有效的 JSON。这意味着当它尝试解析时,会抛出异常。

要解决您的问题,您应该使用removeItem 清除错误值。

localStorage.removeItem("customerDatabase");

【讨论】:

  • 但是为什么 Firefox 给出了一个不错的字符串,而不是未定义的。这个问题只出现在 chrome 中,不是吗?如问题中所述。正如 Dennisboys 对仅打印 chrome 给出未定义的问题所评论的那样。
  • @ALBI 因为这是使用本地存储,所以该值可能来自 before 到代码看起来正确。也许他正在使用 Chrome 进行开发,并且有一次出现了拼写错误,因此 Chrome 的值不正确。到它在 Firefox 中加载时,代码是正确的,因此 Firefox 从未收到损坏的存储值。
  • 好的.. 谢谢 loganfsmyth。
猜你喜欢
  • 2014-04-17
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
相关资源
最近更新 更多