dongren9739 2012-03-23 02:34
浏览 67
已采纳

意外的令牌'<' - AJAX

So I'm trying to output an array to javascript which will tell me if certain streams are online or offline. Every time I try to alert the output it gives me an unexpected token '<' at line 1 of my document. It's driving me nuts. My code is fairly straight forward:

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Streaming</title>
    <style type="text/css">
        *{margin:0px;padding:0px;font-family:Arial}
        #container{margin:0 auto;width: 1000px}
        #player iframe{width:625px;height:510px;}
        #player{float:left}
    </style>
    <script type="text/javascript" src="dynamic.js" ></script>
</head>
<body>
    <div id="container">
        <div id="player">
            <iframe src="streams/tx3fate.html" frameborder="0" scrolling="no"></iframe>
        </div>
        <iframe frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel=day9tv&amp;popout_chat=true" height="500" width="350"></iframe>
    </div>
</body>
</html>

Javascript

function getActive() {
    if(window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {  
            var test = JSON.parse(xmlhttp.responseText);
            window.alert(test);
        }
    }

    xmlhttp.open("GET", "streams.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send('check=true');
}

getActive();

PHP

<?php  
    if($_GET['check'] == true) {
        $streams = array(
            "mxgdichello" => "offline",
            "day9tv" => "offline",
            "tx3fate" => "offline",
            "wochtulka" => "offline",
            "unawaresc2" => "offline",
            "xerse" => "offline",
            "atree2425" => "offline",
            "sc1pio"  => "offline",
            "lokk_2" => "offline",
            "tsremark" => "offline",
            "ognastarcraft" => "offline"
        );

        foreach($streams as $index)    {
            $json_file = @file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$index}", 0, null, null);
            $json_array = json_decode($json_file, true);

            if ($json_array[0]['name'] == "live_user_{$index}") {
                $index = "online";
            } else {
                $index = "offline";
            }
        }

        echo json_encode($streams);
    }
?>

My Iframe html files just contain flash embed objects. I have no idea what is going on - I know that $streams is definitely returning an array so not sure what to do. I am getting the Error in my javascript debugger.

  • 写回答

2条回答 默认 最新

  • douza1373 2012-03-23 02:41
    关注

    It sounds like it is failing to parse your returned json (xmlhttp.responseText) Instead of looking at the value of the response text to see why it cannot be parsed

        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {   
            console.log(xmlhttp.responseText);
            var test = JSON.parse(xmlhttp.responseText);
    

    Try adding the parameter to the url, I think you can only send params with send() for POST requests

     xmlhttp.open("GET", "streams.php?check=true", true);
     xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     xmlhttp.send();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记