responseText로는 잘 받아지는데, responseXML로는 ie에서 데이터가 받아지질 않고 자바스크립트 에러가 나네요.
책을 보고 짠 건데, 책의 소스도 작동을 안하니.. responseXML방식은 ie에선
지원을 안하나요?
Code: Select all
function init()
{
var obj=document.getElementsByTagName("input")[0];
obj.onclick=function()
{
sendRequest(on_loaded1,'','GET','test.txt',true,true);
}
var obj01=document.getElementsByTagName("input")[1];
obj01.onclick=function()
{
sendRequest(on_loaded2,'','GET','index.xml',true,true);
}
}
window.onload=init;
function sendRequest(call_back, data, method, url, param5, param6)
{
var request=createHttpRequest();
request.onreadystatechange=function()
{
if (request.readyState==4)
{
call_back(request);
}
}
request.open(method, url);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(data);
}
//콜백함수 수신시에 실행됩니다.
function on_loaded1(oj)
{
var body=document.getElementsByTagName("body")[0];
if (!document.getElementById("boxcontainer"))
{
var container=document.createElement("div");
container.id="boxcontainer";
container.style.background="yellow";
body.appendChild(container);
}
var result=decodeURIComponent(oj.responseText);
var textnode=document.createTextNode(result);
container.appendChild(textnode);
}
function on_loaded2(oj)
{
var body=document.getElementsByTagName("body")[0];
if (!document.getElementById("container"))
{
var header=document.createElement("h1");
body.appendChild(header);
var container=document.createElement("ol");
container.id="container";
container.style.background="black";
container.style.color="white";
body.appendChild(container);
}
var result=oj.responseXML;
if (header)
{
headertext=result.getElementsByTagName("title")[0].firstChild.nodeValue;
headertext=document.createTextNode(headertext);
header.appendChild(headertext);
}
var items=result.getElementsByTagName("item");
for (v=0; v<items.length; v++)
{
var titles=items[v].getElementsByTagName("title");
for (i=0; i<titles.length; i++)
{
textnode=titles[i].firstChild.nodeValue;
textnode=document.createTextNode(textnode);
liobj=document.createElement("li");
liobj.appendChild(textnode);
container.appendChild(liobj);
}
}
}
function createHttpRequest()
{
if (window.ActiveXObject)
{
try
{
var oj= new ActiveXObject("Msxml.XMLHTTP");
}
catch (e)
{
try
{
var oj= new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
var oj= null;
}
}
}
else if (window.XMLHttpRequest)
{
var oj= new XMLHttpRequest();
}
else
{
var oj= null;
}
return oj;
}