0

我有一个用于获取 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"
        }
    ]
}
4

2 回答 2

1

你知道你可以只存储 JSON 字符串,然后再解析它吗?

function storeUser() {
    return $.ajax({
        type: "GET",
        url: "studio.json",
        dataType: "text"
    });
}

if ( ! localStorage.getItem('user') ) {
    storeUser().done(function(json) {
        user = JSON.parse( json );
        localStorage.setItem('user', json);
    });
}else{
    user = JSON.parse( localStorage.getItem('user') );
}

但是您将不得不等待异步调用再次完成!

MDN 也有一个用于 localStorage 的polyfill,因此它也适用于不支持的浏览器。

于 2013-04-10T00:52:58.477 回答
1

你需要做一些叫做“调试”的事情。重写error处理程序以获取 jQuery 传递给它的参数,然后使用 console.log 打印它们,或者断点并在调试器中检查它们。这是错误的签名:

类型:函数(jqXHR jqXHR,字符串 textStatus,字符串 errorThrown )

请求失败时调用的函数。该函数接收三个参数:jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象、描述发生的错误类型的字符串和可选的异常对象(如果发生)。第二个参数(除了 null)的可能值是“timeout”、“error”、“abort”和“parsererror”。发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。

于 2013-04-10T01:03:00.920 回答