意外的令牌'<'-异步传输

Unexpected Token '<' - AJAX

本文关键字:异步传输 lt 意外 令牌      更新时间:2023-09-26

所以我试图将一个数组输出到javascript,它会告诉我某些流是在线还是离线。每次我试图提醒输出时,它都会给我一个意外的标记"<"在我文件的第1行。它把我逼疯了。我的代码相当直接:

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);
    }
?>

我的Iframe html文件只包含flash嵌入对象。我不知道发生了什么-我知道$streams肯定会返回一个数组,所以不知道该怎么办。我在javascript调试器中得到了Error。

听起来解析返回的json(xmlhttp.responseText)失败了而不是查看响应文本的值来了解为什么不能解析

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

尝试将参数添加到url中,我认为您只能使用send()为POST请求发送参数

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

通常这意味着您的请求失败了,您可能得到的是html页面而不是JSON响应。解析失败,因为它接收的是html而不是JSON对象。验证ajax的目标url是否只返回有效的JSON、任何包装响应的模板系统,或者404是否会给您该响应。

您也可以使用firebug或chrome开发工具,查看XHR请求并查看正文中的内容,如果不是类似于{my_data:"}的内容,那么您可能遇到了问题。

另一点需要注意的是,你不需要文件末尾的"?>",如果你最后多了一行,甚至Zend建议你不要使用它们,它们会导致更多的问题。