JSON请求未完成

时间:2014-09-09 03:50:52

标签: javascript json xmlhttprequest

我是JavaScript的新手,正在尝试使用JSON XMLHttpRequest

根据这本书,Head First:HTML5 Programming,代码需要如下所示:

window.onload = function() {
    var url = "<url here>";
    var request = new XMLHttpRequest();
    request.open("GET", url);
    request.onload = function() {
        if(request.status == 200) {
            updateSales(request.responseText);
        }
    };
    request.send(null);
}

function updateSales(responseText) {
    var salesDiv = document.getElementById("sales");
    salesDiv.innerHTML = responseText;
}

我把我自己的服务器的URL放在JSON文件所在的位置。 JSON文件本身如下所示:

[{"name":"ARTESIA","time":1308774240669,"sales":8},
{"name":"LOS ANGELES","time":1308774240669,"sales":2},
{"name":"PASADENA","time":1308774240669,"sales":8},
{"name":"STOCKTON","time":1308774240669,"sales":2},
{"name":"FRESNO","time":1308774240669,"sales":2},
{"name":"SPRING VA LLEY","time":1308774240669,"sales":9},
{"name":"ELVERTA","time":1308774240669,"sales":5},
{"name":"SACRAMENTO","time":1308774240669,"sales":7},
{"name":"SAN MATEO","time":1308774240669,"sales":1}]

我不知道我在这里失踪了什么,因为我甚至试图复制粘贴。

编辑:

以下是加载脚本的页面:

<!doctype html>
<html lang="en">
    <head>
        <title>Mighty Gumball (JSON)</title>
        <meta charset="utf-8">
        <script src="mightygumball.js"></script>
        <link rel="stylesheet" href="mightygumball.css">
    </head>
    <body>
        <h1>Mighty Gumball Sales</h1>
        <div id="sales">
        </div>
    </body>
</html>

0 个答案:

没有答案