试图从互联网API获取json字符串

时间:2013-07-24 16:30:15

标签: javascript ajax json

我正在尝试从此url获取json字符串: http://status.mojang.com/check

然而,每次我失败或者这样:

XMLHttpRequest cannot load http://status.mojang.com/check. Origin null is not allowed by Access-Control-Allow-Origin. 

任何帮助表示赞赏

<!DOCTYPE html> <html> <head>   <title>mc stats</title>     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>     <meta http-equiv="Access-Control-Allow-Origin" content="*">  </head> <body>

<div id="minecraft"> </div>

<script> (function() {      var flickerAPI = "http://status.mojang.com/check?jsoncallback=?";       $.getJSON(flickerAPI)
        .done(function( data ) {
            console.log(data);  }); })(); </script> </body> </html>

1 个答案:

答案 0 :(得分:0)

您需要在服务器中运行网页,此问题应该消失。谷歌Chrome可能有办法实现,但我不知道。当我遇到此问题时,我通常使用Aptana Studio运行我的文件。它是免费下载的,它使文件从服务器提供,而不是仅从文件系统中读取。它默认为Firefox,但您只需将该网址粘贴到Chrome中,除非出现其他错误,否则所有内容都应按预期运行。

如果您正在运行mac / linux,您还可以访问python简单服务器,它也应该提供服务。从文件目录运行命令python -m SimpleHTTPServer,您应该可以在localhost:8000看到它。祝你好运!