2014-05-23 3 views
0

요즘 AJAX와 XML을 배우고 있습니다. 최근 바보 같은 문제에 빠져 들었습니다.로컬 서버에서 xmlHttp.responseXML을 실행할 수 없습니다.

나는 입력 상자에 입력 한 모든 것을 <div>에 표시 할 간단한 프로그램을 만들려고합니다. .responseXML 속성을 사용하려고 할 때 어떤 이유로 프로그램이 실행되지 않습니다. .responseText을 사용할 때 모든 것이 올바르게 작동합니다.

내 HTML 코드 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Untitled Document</title> 
     <script type="text/javascript" src="foodstore.js"></script> 
    </head> 
    <h3> the chuff bucket </h3> 
    <body onload="process()"> 

     <input type="text" id="userInput"/> 
     <div id="underInput"></div> 
    </body> 
</html> 

내 PHP 코드 :

<?php 
header('Content-Type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 
echo '<response>'; 
$food=$_GET['food']; 
echo $food; 
echo '</response>'; 
?> 

내 JS 코드 :

// JavaScript Document 
var xmlHttp= createXmlHttpRequestObject(); 



function createXmlHttpRequestObject(){ 
    var xmlHttp; 


    if(window.ActiveXObject){ 
     try{ 
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     }catch(e){ 
      xmlHttp = false; 
    } 


    } else { 
     try{ 
      xmlHttp = new XMLHttpRequest(); 
     }catch(e){ 
      xmlHttp = false; 
     } 
    } 

    if(!xmlHttp){ 
     alert("cant create object"); 
    }else{ 
     return xmlHttp; 
    } 
} 



function process(){ 
    var x = xmlHttp.readyState 
    if(xmlHttp.readyState==0||xmlHttp.readyState==4){ 

     food = encodeURIComponent(document.getElementById("userInput").value) 
     xmlHttp.open("GET","foodstore.php?food=" + food , true); 
     x=xmlHttp.readyState; 
     xmlHttp.onreadystatechange= handleServerResponse; 
     xmlHttp.send(null); 

    }else{ 
     setTimeout('process()',1000); 
    } 
} 


function handleServerResponse(){ 

    if(xmlHttp.readyState==4){ 
     if(xmlHttp.status==200){ 

     var xmlResponse= xmlHttp.responseXML; 
      root = xmlResponse.documentElement; 
      alert(root.firstchild); 
      //message= root.firstChild.data; 

      document.getElementById("underInput").innerHTML= '<span  style="color:blue">' + xmlResponse + '</span>'; 
      setTimeout('process()', 1000); 
     }else{ 
      alert('not working'); 
     } 
    } 
} 

덕분에 헬퍼.

답변

0

jQuery를 사용해 보셨습니까? 할 수 있습니다. 데이터 유형을 지정할 수 있습니다

function ajaxJQueryRequest(url,afterReqFunction){ 
    var request = $.ajax({ 
     url: url, 
     type: "GET", 
     crossDomain: true, 
     dataType: "xml" 
     }); 
    request.done(function(msg) { 
      afterReqFunction(msg); 
     }); 
    request.fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
     }); 
} 
+0

단계별로 배우고 싶기 때문에 고마워하지만 내 방식대로하고 싶습니다. – misha312

+0

handleServerResponse()에서 수행하려고 시도한 결과는 다음과 같습니다. console.log (xmlHttp.responseXML); 콘솔인지 알려주세요. – user3667931

0

확인 버그를 발견 한 것 같습니다. 열 여섯에서 2 행에

오류 : 는 "이 페이지는 다음과 같은 오류가 포함되어 XML 선언은 단지에서 허용 나는 (HTML로 응답을 보내는 것을)와하면 다음과 같은 오류가 발생했습니다 PHP 파일의 URL을 입력 문서의 시작 "

내 PHP의 상단에 빈 공간을 삭제 한 후에 새로운 오류가 발생하여 모든 오류가 수정 된 후 모든 것이 매력처럼 작동했습니다 ... 감사합니다.

관련 문제