2012-03-23 5 views
0

그래서 어떤 스트림이 온라인 또는 오프라인인지 알려주는 배열을 자바 스크립트로 출력하려고합니다. 출력을 알리려고 할 때마다 내 문서의 줄 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> 

자바 스크립트

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 파일에 플래시 삽입 객체가 포함되어 있습니다. 무슨 일이 벌어지고 있는지 전혀 모르겠다. 나는 $streams이 확실히 배열을 반환하므로 무엇을해야할지 모른다. 내 자바 스크립트 디버거에서 오류가 나타납니다. 이에 매개 변수를 추가

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

시도를 구문 분석 할 수없는 이유는 볼 대신 응답 텍스트의 값을보고의 를 반환 된 JSON (xmlhttp.responseText를) 구문 분석하는 데 실패처럼

+0

요청에 대한 응답은 무엇입니까? 네트워크 패널 (예 : 자주 이용하는 개발자 도구 패널 참조). 문제를 일으키는 경고가 표시 될 수 있습니다. – Ryan

+0

정확하게 "예기치 않은 토큰"오류 메시지를주는 것은 무엇입니까? 브라우저? 자바 스크립트? PHP? – deceze

+0

자바 스크립트에서 내 오류가 발생했습니다. 내 질문을 업데이트했습니다. 죄송합니다. –

답변

2

소리가 난다 URL은, 나는 일반적으로 그건 당신이 요청이 실패하고 당신은 아마 HTML 페이지 대신 JSON 응답을 얻고있어 것을 의미 만 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에서 누락 된 부분이 있습니까? –

+0

체크 파라메타를 이동하십시오 (편집 된 답변 참조) – objects

+0

안녕하세요! 그게 효과가 : D 조 감사! –

0

를 요청합니다. Parse는 JSON 객체가 아닌 html을 받기 때문에 실패합니다. ajax의 대상 URL이 유효한 JSON, 응답을 래핑하는 템플릿 시스템 만 반환하거나 404가 해당 응답을 제공하는지 확인합니다.

방화범이나 크롬 개발자 도구를 사용해도 XHR 요청을보고 본문에있는 내용을 볼 수 있습니다. {my_data : ''}가 아니라면 문제가있을 수 있습니다.

파일 끝 부분에 "?>"이 필요하지 않으므로 여분의 줄로 끝나면 가치가없는 것보다 더 많은 문제가 발생하고 Zend도 사용하지 말 것을 권한다.

+0

또한 확인이 통과되고 있는지 확인하십시오. – Chris

관련 문제