【发布时间】:2011-08-18 02:26:51
【问题描述】:
刚接触 javascript,我想我会尝试从数据库中检索一些信息(我希望最终绘制图表!)。
<!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>
如果我在浏览器窗口中键入,“http://localhost:8000/activity?starttime=13129788041&endtime=0&hostid=1”行会返回我要查找的内容:
{"1.313496422E9":[0.21000000000003638,448,12754,1.868579977195076]}
但在 javascript 中不起作用。
如果我用 httpfox 看有一个错误
应用程序/xml (NS_ERROR_DOM_BAD_URI)
但如果我在 httpfox 中查看“内容”选项卡,我实际上可以看到我需要的数据,但存在 XML 解析错误。
<?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>
NS_ERROR_DOM_BAD_URI 似乎与跨域问题有关,但我在实际服务器上本地工作,所以我应该能够访问数据吗?另外,当我可以在 httpfox 的“内容”选项卡中看到我想要的确切数据时,为什么会出现 XML 解析错误?
我可能正在做一些非常业余的事情,但欢迎任何帮助/想法/滥用!
干杯,
罗伯。
【问题讨论】:
-
听起来您的服务器端脚本由于某种原因正在返回带有
Content-Type: application/xml标头的 JSON 数据。它不应该那样做。application/json不太可能让 AJAX 库出错。 -
我终于设法更改了服务器端的脚本,现在它返回 Content-Type: application/json,我可以在 httpfox 的内容选项卡中看到我的 JSON 数据,没有错误。但是,即使我在使用“localhost”或“127.0.0.1”的实际服务器本身上,我仍然会收到 NS_ERROR_DOM_BAD_URI 错误。