Luuk D. Jansen
Luuk D. Jansen

Reputation: 4496

Json request not executed properly

Maybe somebody can help me, as I cannot see what I am doing wrong. I have a simple page which contains the following:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <script src="@{'/public/javascripts/jquery-1.5.2.min.js'}" type="text/javascript"></script>
    <script>

function startTime() {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('clock').innerHTML = h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}

function checkTime(i) {
    if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}

function loadData(){
    alert('test1');

    $.getJSON( "http://localhost:9000/carousel/1/data", function( data ) {
        alert('test2');
    });
}

</script>

</head>
<body onload="loadData()">
<div id="wrapper">
    <h1>Test</h1>
</div>
<div id="name">${category?.name}</div>
<div id="clock"></div>
<div class="custom"></div>
</body>
</html>

The loadData() functions is called, I see 'test1', but although Safari shows that 'data' is downloaded, the 'test2' alert never pops up. I think I have done this millions of times on other pages, so I must be forgetting something small.

Anybody who can point it out to me?

Upvotes: 0

Views: 55

Answers (1)

manji
manji

Reputation: 47978

If the json file is malformatted, the query will fail silently.

From http://api.jquery.com/jquery.getjson/:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

Upvotes: 1

Related Questions