我有一个用于获取 json 文件的 javascript 文件,如果它成功,它将执行一个功能,如果不是,它会显示一个警告窗口,显示“错误”,但虽然语法看起来正确,但我得到一个每次都有错误的警报窗口。
这是两个文件
var firstName = [];
var lastName = [];
var email = [];
var password = [];
var i;
function insertRecord() {
$.ajax({
type: "GET",
url: "studio.json",
dataType: "json",
success: insertData,
error: function(){ alert("error"); }
});
function insertData(data) {
if (localStorage) {
for (i = 0; i < data.User.length; i++) {
localStorage.setItem(firstName[i], data.User.firstName[j]);
localStorage.setItem(lastName[i], data.User.lastName[j]);
localStorage.setItem(email[i], data.User.email[j]);
localStorage.setItem(password[i], data.User.password[j]);
}
localStorage.setItem(firstName[i], document.getElementById("firstName").value);
localStorage[firstName[i]] = document.getElementById("firstName").value;
localStorage.setItem(lastName[i], document.getElementById("lastName").value);
localStorage[lastName[i]] = document.getElementById("lastName").value;
localStorage.setItem(email[i], document.getElementById("email").value);
localStorage[email[i]] = document.getElementById("email").value;
localStorage.setItem(password[i], document.getElementById("password").value);
localStorage[password[i]] = document.getElementById("password").value;
alert(localstorage[firstName]);
}
else
alert("Your browser does not support Local Storage");
}
}
json 文件称为 studio.json
{
"Students":[
{ "first":"Arnold", "last":"Matthews", "class":"Tiny Tutus" },
{ "first":"Keesha", "last":"Franklin", "class":"Breakdancing"},
{ "first":"Dorothy", "last": "Ann", "class":"Acro"},
{ "first":"Wanda", "last": "Li", "class":"Ballet"},
{ "first":"Phoebe", "last": "Terese", "class":"Tap"}
],
"Classes":[
{ "name":"Tiny Tutus", "time":"450","AMPM":"PM", "students":12},
{ "name":"Breakdancing", "time":"230","AMPM":"PM", "students":6},
{ "name":"Acro", "time":"810","AMPM":"AM", "students":15},
{ "name":"Tap", "time":"520","AMPM":"PM", "students":11},
{ "name":"Ballet", "time":"620","AMPM":"PM", "students":15},
{ "name":"Jazz", "time":"740","AMPM":"AM", "students":8 }
],
"User":[
{
"firstName": "Michael",
"lastName": "Jones",
"username": "MJone",
"email": "Michael.Jones@email.com",
"password": "qwerty"
},
{
"firstName": "Michelle",
"lastName": "Lee",
"username": "MLee",
"email": "Michelle.Lee@gmail.com",
"password": "asdfgh"
},
{
"firstName": "Andrew",
"lastName": "Bob",
"username": "ABob",
"email": "Andrew.Bob@hotmail.com",
"password": "zxcvbn"
}
]
}