解析JSON http请求时遇到问题

时间:2014-08-29 01:58:36

标签: javascript php html ajax json

我在w3schools.com上引用了教程中的代码。我不知道自己可能做错了什么,但是当我测试网站时,我没有输出。没有任何。我将发布以下代码。

<!DOCTYPE html>
<html>
    <body>
        <p id="par1"></p>
        <script>
            var xmlhttp = new XMLHttpRequest();
            var url = "http://xproshowcasex.api.channel.livestream.com/2.0/livestatus.json?callback=status";

            xmlhttp.onreadystatechange=function() {
                //readyState 4: request finished and response is ready
                //status 200: "OK"
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    myFunction(xmlhttp.responseText);
                }
            }
            //var 'url' is defined above
            xmlhttp.open("GET", url, true);
            xmlhttp.send();

            function status(response) {
                var arr = JSON.parse(response);

                if (arr.isLive = true) {
                    document.getElementById("par1").innerHTML = "live"; 
                } else {
                    document.getElementById("par1").innerHTML = "offline";
                }
            }
        </script>
    </body>
</html>

我在Chrome上检查了控制台日志,并给了我这个错误:

XMLHttpRequest cannot load http://xproshowcasex.api.channel.livestream.com/2.0/livestatus.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我在论坛上查看了直播以及其他地方,没有人可以为我提供可靠的解决方案。希望有人可以。谢谢你的帮助!

- 的修改 -

我搜索了这个网站,但没有找到解决我问题的方法。如果有人知道哪里可能有解决方案,请发布一个链接,但据我所知,没有。众所周知,不同的代码有不同的问题,所以我很感激答案,而不是[重复]标记。

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
<head>
<meat charset="utf-8">
<title>test</title>


<script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>

<!--Livestream status script-->
<!-- CAN BE PLACED IN BODY -->
<script type="text/javascript">
$(document).ready(function () {

function getCrossDomainJson(url, callback) {
    $.ajax({
        url: "http://query.yahooapis.com/v1/public/yql?callback=?",
        data: {
            q: 'select * from xml where url="' + url + '"',
            format: "json"
        },
        dataType: "jsonp",
        success: callback
    });
}

/*INSERT STREAM NAME INBETWEEN 'x' eg. http://xSTREAMNAMEx.channel-api.livestream-api.com/2.0/getstream*/
getCrossDomainJson("http://xhellgateospreyx.channel-api.livestream-api.com/2.0/getstream", function(data) {
    console.dir(data);

    if (data && data.query && data.query.results && data.query.results.channel) {
        var statusTest = data.query.results.channel.isLive;


        if (statusTest == "true") {
            document.getElementById("par1").innerHTML = "online";
        }

        else {
            document.getElementById("par2").innerHTML = "offline";
        }

    }
});

});
</script>

<!-- end of script -->

<body>

//par1 will change to online if stream is online; par2 will remain as unaffected
//par2 will change to offline if stream is offline; par1 will remain as unaffected
//I separated the p tags for testing purposes, but they can be combined
//if you do so, change the id in the if/else statements
<p id="par1">unaffected</p>
<p id="par2">unaffected</p>




</body>
</html>

如果您想要多个流状态,请从&#39; getCrossDomainJson&#39;中选择代码。直到第一个&#39;});&#39;并粘贴在两个&#39;})之间;&#39;并在&#39; getElementById&#39;中替换流名称和标记。感谢所有帮助我解决这个问题的人!希望这有助于其他人。