getJSON 导致 XML 解析错误?

发布于 2024-11-29 20:45:45 字数 2107 浏览 0 评论 0原文

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 查看,则会出现错误

application/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 解析错误?

我可能正在做一些非常业余的事情,但任何帮助/想法/滥用都会受到欢迎!

干杯,

罗布。

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 look 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2024-12-06 20:45:45

您来自服务器的 json 格式不正确,或者......您实际上没有使用 jQuery.getJSON 函数。如果要使用jQuery.get,则需要指定json数据类型作为最后一个参数。

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');

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文