Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我总是得到;未捕获的语法错误:意外标记u“;来自Chrome?_Javascript_Json - Fatal编程技术网

Javascript 为什么我总是得到;未捕获的语法错误:意外标记u“;来自Chrome?

Javascript 为什么我总是得到;未捕获的语法错误:意外标记u“;来自Chrome?,javascript,json,Javascript,Json,我花了整整一天的时间在这上面,在谷歌上搜索答案,但仍然无法找到答案 我的代码有点长,在Firefox中运行良好,但从Chrome获得了“未捕获的SyntaxError:意外标记u” 有人能指出我错在哪里吗?提前谢谢 // when the page loads, list all the current contacts $(document).ready(function(){ // check if localStorage database exists if(!loca

我花了整整一天的时间在这上面,在谷歌上搜索答案,但仍然无法找到答案

我的代码有点长,在Firefox中运行良好,但从Chrome获得了“未捕获的SyntaxError:意外标记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();


});
//页面加载时,列出所有当前联系人
$(文档).ready(函数(){
//检查本地存储数据库是否存在
如果(!localStorage.getItem(“customerDatabase”)){
//定义一个JSON对象来保存所有当前地址
var触点={
“用户”:[
{
“id”:“1”,
“姓名”:“丹尼斯”,
“电子邮件”:dennisboys@gmail.com"
},
{
“id”:“2”,
“姓名”:“佐伊”,
“电子邮件”:zoeisfemale@gmail.com"             
}
]   
}//联系人JSON对象结束
//将对象字符串化
var stringObject=JSON.stringify(联系人);
//将其存储到本地存储数据库中
var storedDatabase=localStorage.setItem(“customerDatabase”,stringObject);
}否则{
//在页面加载时列出所有客户
listJSONCustomers();
}   
//列出localStorage中JSON对象中的所有当前联系人
函数listJSONCustomers(){
var displayHTML=“”;
var i;
//从本地存储获取数据
var storedDatabase=localStorage.getItem(“customerDatabase”);
//将数据从字符串解析为JSON对象
var parseObject=JSON.parse(storedDatabase);
//访问JSON对象的用户密钥
var userObject=parseObject.users;
//获取对象的长度(数据库有多少客户)
var contactsLength=userObject.length;

对于(i=0;i,在某个点上,您所做的某件事损坏了该键的LocalStorage的值。LocalStorage只能存储字符串,因此,如果您向其传递任何其他内容,它将将将其转换为字符串。由于您的值是
'undefined'
,这意味着在某个点上,您可能在意外情况下做了如下操作:

var value;
localStorage.setItem('key', value);
在这种情况下,
未定义的
,它不是一个字符串。保存时,它将被转换。不幸的是,
未定义的
不是有效的JSON。这意味着当它尝试解析时,它将抛出异常

要解决问题,应使用
removietem
清除错误值

localStorage.removeItem("customerDatabase");

如果我复制并跳过此代码,则不会在chrome中出现此错误。您确定此错误属于此文件,而不是扩展名吗?可能您的本地存储中保存了以前的损坏内容?如果在调用
parse()之前打印字符串
,看起来正确吗?根据错误,localstorage值在“users”键周围没有引号。@loganfsmyth,我在调用parse()之前打印了字符串,Firefox返回一个很好的字符串,但Chrome返回“undefined”。但这是怎么回事?@Dennisboys这意味着您在某个时候意外地将undefined写入了localStorage。@loganfsmyth谢谢!我使用localStorage.remove(),它在Chrome中也能很好地工作。=D!!你救了我一天!再次感谢!但是为什么firefox给出了一个漂亮的字符串,而不是未定义的字符串。这个问题只出现在Chrome中,没有?正如问题中提到的。正如Dennisboys对只打印Chrome时给出未定义字符串的问题所做的评论一样。@ALBI因为这是使用本地存储,所以值可以是f之前的rom代码看起来是正确的。也许他是在Chrome中开发的,有一次输入错误,所以Chrome的值不正确。当它加载到Firefox中时,代码是正确的,所以Firefox从未得到损坏的存储值。好的..谢谢loganfsmyth。