2012-03-23 88 views
0

所以我试图输出一个数组为javascript,它会告诉我,如果某些流在线或离线。每次我尝试提醒输出时,它都会在我的文档的第1行给我一个意外的标记'<'。这让我疯狂。我的代码是相当直截了当:意外令牌'<' - AJAX

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调试器中的错误。

+0

对请求的回应是什么? (请参阅网络面板,例如,在您最喜爱的开发人员工具面板中。)可能存在导致此问题的警告。 – Ryan 2012-03-23 02:35:22

+0

究竟是什么给你的“意外令牌”的错误消息?浏览器? JavaScript的? PHP? – deceze 2012-03-23 02:36:06

+0

我得到我的错误在JavaScript中,更新我的问题,对不起。 – 2012-03-23 02:37:08

回答

2

这听起来像它无法解析您的返回的JSON(xmlhttp.responseText) 而不是着眼于响应文本的价值,看看它为什么不能被解析

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

尝试添加参数该网址,我想你只能发送()用于发送POST请求PARAMS

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

好的,我做到了,我只是空了一行。我假设这意味着我的PHP没有返回任何东西,但如果我尝试回显该数组的值,它工作正常。在我将它发回给我的Javascript文件之前,我在PHP中丢失了什么? – 2012-03-23 02:44:01

+0

尝试移动选中的参数(请参阅编辑答案) – objects 2012-03-23 23:04:03

+0

嘿!这工作:D谢谢! – 2012-03-23 23:34:22

0

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

你也可以使用firebug或chrome开发者工具,看看你的XHR请求,看看里面有什么,如果它不是像{my_data:'}}那么你可能有问题。

另一个注意事项是,在文件末尾不需要“?>”,如果最后有一行,Zend建议您不要使用它们,否则会导致更多的问题。

+0

也验证检查也通过。 – Chris 2012-03-23 02:54:04