getJSON results in XML parse error?
New to javascript and thought I would try retrieve some information from a database (and eventually graph it I hope!).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Flot Examples</title>
<link href="layout.css" rel="stylesheet" type="text/css">
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="../jquery.js"></script>
<script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
</head>
<body>
<h1>Test Javascript:</h1>
<script type="text/javascript">
$(function () {
$.get("http://localhost:8000/activity?starttime=13129788041&endtime=0&hostid=1", function(data) {
$.each(data, function(i,item){
document.write(i);
});
});
});
</script>
</body>
</html>
The line "http://localhost:8000/activity?starttime=13129788041&endtime=0&hostid=1" returns what I'm looking for if I type it in a browser window:
{"1.313496422E9":[0.21000000000003638,448,12754,1.868579977195076]}
But doesn't work in the javascript.
If I 开发者_如何学Pythonlook with httpfox there is an error
application/xml (NS_ERROR_DOM_BAD_URI)
But if I view the "contents" tab in httpfox I can actually see the data under I require but there is an XML parse error.
<?xml-stylesheet
href="chrome://global/locale/intl.css" type="text/css"
?>
<parsererror>
XML Parsing Error: not well-formed Location: chrome://browser/content/browser.xul Line Number 1, Column 1:
<sourcetext>
{"1.313496422E9":[0.21000000000003638,448,12754,1.868579977195076]} ^
</sourcetext>
</parsererror>
The NS_ERROR_DOM_BAD_URI seems to be about cross domain issues but I'm working locally on the actual server so should I be able to access the data? Also why is there an XML parse error when I can see the exact data I want in httpfox's "Content" tab?
I'm probably doing something horrendously amateur but any help/thoughts/abuse would be welcome!
Cheers,
Rob.
Either your json is malformed coming from the server or....you aren't actually using the jQuery.getJSON
function. If you want to use jQuery.get
, you need to specify the json data type as the last parameter.
http://api.jquery.com/jQuery.getJSON/
$.get("http://localhost:8000/activity?starttime=13129788041&endtime=0&hostid=1", function(data) {
$.each(data, function(i,item){
document.write(i);
});
}, 'json');
精彩评论