|
<!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" xml:lang="ko" lang="ko"> <head> <meta http-equiv="content-type" content="text/html; charset=euc-kr" /> <title>XML로부터 객체 생성</title> <script type="text/javascript "> var ajax = {}; ajax.xhr = {}; ajax.xhr.Request = function(url, params, callback, method) { this.url = url; this.params = params; this.callback = callback; this.method = method; this.send(); } ajax.xhr.Request.prototype = { getXMLHttpRequest: function() { if (window.ActiveXObject) { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e1) { return null; } } } else if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { return null; } }, send: function() { this.req = this.getXMLHttpRequest(); var httpMethod = this.method ? this.method : 'GET'; if (httpMethod != 'GET' && httpMethod != 'POST') { httpMethod = 'GET'; } var httpParams = (this.params == null || this.params == '') ? null : this.params; var httpUrl = this.url; if (httpMethod == 'GET' && httpParams != null) { httpUrl = httpUrl + "?" + httpParams; } this.req.open(httpMethod, httpUrl, true); this.req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded'); var request = this; this.req.onreadystatechange = function() { request.onStateChange.call(request); } this.req.send(httpMethod == 'POST' ? httpParams : null); }, onStateChange: function() { this.callback(this.req); } } window.onload = function() { new ajax.xhr.Request("http://kr.open.gugi.yahoo.com/service/rgc.php", "appid=VyY1f0DV34G8HA3UnPbygP8VofLY38zM57lJ7wC7HS3qyxGTBiqrfKa7Qqpetfw&latitude=37.4997677193116&longitude=129.0294189453125&output=xml", viewInfo, "GET"); } function viewInfo(req) { if (req.readyState == 4) { if (req.status == 200) { var docXML = req.responseXML; var code = docXML.getElementsByTagName("Found") .item(0).firstChild.nodeVal ue; if (code == '1') { var country = docXML.getElementsByTagName("country") .item(0).firstChild.nodeVal ue; var state = docXML.getElementsByTagName("state") .item(0).firstChild.nodeVal ue; var county = docXML.getElementsByTagName("county") .item(0).firstChild.nodeVal ue; var town = docXML.getElementsByTagName("town") .item(0).firstChild.nodeVal ue; alert(country+state+county+town); } else { alert("실패"); } } else { alert("에러 발생:"+req.status); } } } </script> </head> <body> </body> </html>
브라우져에서xml로 결과가나오는데 이것을 돔을사용해서 접근해서 정보를 가져오려고 하는데 잘안되네요 브라우져실행하면 접근권한이없다고 나오는데 잘알려주세요
|