XHR을 수행하는 방법을 배우고 웹 서버 (tomcat7)에서 w3school의 코드를 사용해보십시오. HTML과 같은 파일에 "note.xml"을 배치하면 아래 코드를 사용하여 XML 파일을 가져올 수 있습니다.다른 서버에서 파일을 가져 오는 방법은 무엇입니까?
xmlhttp.open("GET","note.xml",true);
그러나 "note.xml"을 다른 웹 응용 프로그램 위치로 이동하면 동일한 방법을 사용할 수 없습니다. 또한 아래 코드는 XML 파일을 가져올 수 없습니다.
xmlhttp.open("GET","http://localhost:8080/anotherWEBapp/note.xml",true);
HTML :
<html>
<body>
<h1>W3Schools Internal Note</h1>
<div>
<b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://localhost:8080/test-app/note.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
는 난 단지 자바 스크립트의 사용과 XML 파일을 얻을 수 있어요 어쨌든이 있나요?
다음 참조 자료를 읽으십시오. http://en.wikipedia.org/wiki/Same_origin_policy –