A variety of JSON problems
I've been banging my head against a wall all day trying to sort this out.
I'm making a JSON request thus:
-
$.getJSON(json_url,function(data){
$("#lightboxbody").html('<script type="text/javascript" src="'+data.jssource+'"></script>');
$.each(data.comments, function(comment){
$("#lightboxbody").append("<div class='comment'><div class='userbox'><span class='colourspan' style='background:#"+comment.colour+";' > </span> "+comment.username+" @ "+comment.timestamp+"</div><div class='content'>"+comment.content+"</div></div>");
});
$("#lightboxbody").append(data.form);
});
So getting results from the url, and putting it all into my lightbox. Here's a sample of the JSON data I'm dealing with:
-
{
"jssource": "http://www.domain.com/js/morescript.js",
"comments": [
{
"content": "comment",
"timestamp": "01:53:18, 20/7/2009",
"username": "tom",
"colour": "d17e82"
}
],
"form": "Login to comment"
}
Which validates ok. Now when I run this script, I get all my comments as just showing undefined for all values.
Secondly, this code doesn't work if it's on a different domain to that of the url it's requesting from. Pasting in the flickr example from the jQuery docs into my file works, but my code doesn't, if I put an alert() in as the first line of the callback function it never triggers. I can't think of anything that would cause this. If I copy and paste the generated json code from the flickr page onto a static page on my server (still different from the one it's running on) then it works ok.
Some bizarre behaviour from Firebug in this case: The call triggers and gets a status 200 OK. It loads up further stuff, like images etc. on the Net console, but these never reach the page. It's just left with my ajax loading whizzer.
If you need any further explanation just ask. I'm completely stumped by this one.