मैंने इस पर पूरे दिन बिताया है, गुगलिंग और उत्तर खोज रहे हैं लेकिन अभी भी पता नहीं लगा सका।क्रोम से मुझे हमेशा "अनकॉट सिंटेक्स त्रुटि: अप्रत्याशित टोकन यू" क्यों मिलता है?
मेरा कोड थोड़ा लंबा है और यह फ़ायरफ़ॉक्स में अच्छी तरह से काम करता है लेकिन क्रोम से "अनचाहे सिंटेक्स त्रुटि: अप्रत्याशित टोकन यू" प्राप्त करता है।
क्या कोई मुझे बता सकता है कि मैं गलत हूं? अग्रिम में धन्यवाद!
// 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":"[email protected]"
},
{
"id":"2",
"name":"zoe",
"email":"[email protected]"
}
]
} // 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();
});
मुझे क्रोम में यह त्रुटि नहीं मिलती है अगर मैं इस कोड को कॉपी और पेस्ट करता हूं। क्या आप सुनिश्चित हैं कि त्रुटि इस फ़ाइल से संबंधित है, और उदाहरण के लिए नहीं एक विस्तार? –
हो सकता है कि आपके पास पहले से ही आपके स्थानीय स्टोरेज में कुछ दूषित बचाया गया हो? यदि आप 'parse()' को कॉल करने से पहले स्ट्रिंग प्रिंट करते हैं, तो क्या यह सही दिखता है? त्रुटि के आधार पर, स्थानीय स्टोरेज मान में आपके "उपयोगकर्ता" कुंजी के चारों ओर उद्धरण नहीं हैं। – loganfsmyth
@loganfsmyth, मैंने पार्स() को कॉल करने से पहले स्ट्रिंग मुद्रित की, फ़ायरफ़ॉक्स एक अच्छी स्ट्रिंग देता है लेकिन क्रोम 'अपरिभाषित' देता है। लेकिन वह कैसा है? – Dennisboys