2013-04-10 27 views
0

我有一個是應該檢索本地JSON文件,並把它解析和字符串化到本地存儲該HTML代碼。我想測試的代碼,但是當我填寫表格並運行它,它說的ReferenceError:insertRecord沒有在Firefox控制檯中定義。的ReferenceError:insertRecord沒有定義

任何幫助將是非常讚賞。 謝謝。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Register</title> 
<link href="css/navigation.css" rel="stylesheet" type="text/css"> 
<link href="css/body.css" rel="stylesheet" type="text/css"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" /> 
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script> 
<script src="store.js" type="text/javascript"></script> 
<script type="text/javascript"> 
var i; 
var user; 
var txt; 

$(document).ready(function() { 
    $.ajax({ 
    type: "GET", 
    url: "studio.json", 
    dataType: "json", 
    success: getUser, 
    error: function() {alert("retrieval error"); } 
}); 
}); 

function getUser(data) { 
if (localStorage) { 
    if (!localStorage.getItem('user')) { 
     user = JSON.parse(data.User); 
     localStorage.setItem('user', user); 
    } 

    else { 
     String item = localStorage.getItem('user'); 
     user = JSON.parse(item); 
    } 
} 

else 
    alert("Local Storage not supported"); 
} 

function insertRecord() { 
$.ajax({ 
    type: "GET", 
    url: "studio.json", 
    dataType: "json", 
    success: insertUser, 
    error: function(){ alert("retrieval error"); } 
}); 

function insertUser() { 
    txt = { firstName: document.getElementById("firstName").value, 
      lastName: document.getElementById("lastName").value, 
      userName: document.getElementById("userName").value, 
      email: document.getElementById("email").value, 
      password: document.getElementById("password").value 
      }; 
    localStorage.setItem('user', user + JSON.stringify(txt)); 
} 
} 

</script> 
</head> 

<body> 
<img src="images/Dance etc logo LARGE.jpg" width="263" height="150" alt="Dance Etc Logo" /> 
<hr /> 
<div data-role="header"> 
    <h1>Dance Etc.</h1> 
</div> 

<div data-role="content" > 
    <h1>Register</h1> 
    <form method="post" action="" id="register" name="form"> 
    <label>First Name: </label> 
    <input type="text" id="firstName" /><br /> 
    <label>Last Name: </label> 
    <input type="text" id="lastName" /><br /> 
    <label>Username: </label> 
    <input type="text" id="userName" /><br /> 
    <label>Email: </label> 
    <input type="text" id="email" /><br /> 
    <label>Password: </label> 
    <input type="password" id="password" /><br /> 
    <input type="submit" value="Register" onClick="insertRecord()" /> 
    </form> 
</div> 

<div data-role="footer"> 
    <a href="Dance_Program">Contact Us</a> 
    <a href="Register">Policy</a> 
    <a href="Login">History</a> 
</div> 

回答

0

在你的代碼有下面這行:

String item = localStorage.getItem('user'); 

這不是有效的JavaScript,因此被扔SyntaxError: Unexpected identifier,防止你的JavaScript代碼的其餘部分被評估。
但是,當您嘗試在HTML中內聯insertRecord時,它會在觸發時嘗試運行,但無法調用未經評估的代碼。

您應該考慮安裝使用element.addEventListener事件偵聽器。 此外,縮進你的代碼可以幫助你調試了。

var i, user, txt; 

$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "studio.json", 
     dataType: "json", 
     success: getUser, 
     error: function() { 
      alert("retrieval error"); 
     } 
    }); 
}); 

function getUser(data) { 
    if (localStorage) { 
     if (!localStorage.getItem('user')) { 
      user = JSON.parse(data.User); 
      localStorage.setItem('user', user); 
     } else { 
      item = localStorage.getItem('user'); 
      user = JSON.parse(item); 
     } 
    } else 
     alert("Local Storage not supported"); 
} 

function insertRecord() { 
    $.ajax({ 
     type: "GET", 
     url: "studio.json", 
     dataType: "json", 
     success: insertUser, 
     error: function() { 
      alert("retrieval error"); 
     } 
    }); 

    function insertUser() { 
     txt = { 
      firstName: document.getElementById("firstName").value, 
      lastName: document.getElementById("lastName").value, 
      userName: document.getElementById("userName").value, 
      email: document.getElementById("email").value, 
      password: document.getElementById("password").value 
     }; 
     localStorage.setItem('user', user + JSON.stringify(txt)); 
    } 
} 
+0

@Paul_S。你好,謝謝你的迴應。我試着運行的項目= localStorage.getItem(「用戶」),但是它告訴我語法錯誤:JSON.parse:意外的字符。 – 2013-04-10 17:14:46

+0

這意味着'data.User'不包含有效的JSON。請確認您的JSON有效,例如在http://jsonlint.com/ – 2013-04-10 18:54:55

+0

@Paul_S。哦,你是對的。非常感謝你 – 2013-04-10 19:58:02