【问题标题】:Unexpected Token '<' - AJAX意外的令牌'<' - AJAX
【发布时间】:2012-03-23 02:34:10
【问题描述】:

所以我试图将一个数组输出到 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 调试器出现错误。

【问题讨论】:

  • 对请求的响应是什么? (请参阅网络面板,例如在您最喜欢的开发人员工具面板中。)可能有导致问题的警告。
  • 究竟是什么给了您“意外令牌”错误消息?浏览器? Javascript? PHP?
  • 我在 javascript 中遇到错误,更新了我的问题,抱歉。

标签: php javascript html ajax json


【解决方案1】:

听起来它无法解析您返回的 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();

【讨论】:

  • 好的,我这样做了,但我得到了一个空行。我假设这意味着我的 PHP 没有返回任何内容,但是如果我尝试回显该数组的值,它可以正常工作。在将 PHP 发送回 Javascript 文件之前,我是否遗漏了一些内容?
【解决方案2】:

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

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

另外请注意,您不需要在文件末尾添加“?>”,如果您最终多出一行,它们会导致更多问题,甚至 Zend 建议您不要使用它们。

【讨论】:

  • 还要验证检查是否也通过了。
猜你喜欢
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
相关资源
最近更新 更多