310 likes | 504 Views
AJAX. 보통 page request. Page Reload. AJAX – A synchronous J avascript & X ML. XMLHttpRequest object. XMLHttpRequest object. Properties: readyState responseXML Methods: open send Event: onreadystatechange. XMLHttpRequest. Properties: readyState responseXML Methods: open
E N D
보통 page request Page Reload
AJAX – Asynchronous Javascript & XML XMLHttpRequest object
XMLHttpRequest object • Properties: • readyState • responseXML • Methods: • open • send • Event: • onreadystatechange
XMLHttpRequest • Properties: • readyState • responseXML • Methods: • open • send • Event: • onreadystatechange readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Properties: • readyState • responseXML • Methods: • open • send • Event: • onreadystatechange readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete Event fires 4 times!
XMLHttpRequest • Initialize: var xmlHttp; function sendAjax(){ xmlHttp=new XMLHttpRequest(); xmlHttp.onreadystatechange = respAjax; … readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Initialize: function respAjax(){ if(xmlHttp.readyState == 4{ 결과를 처리} readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Initialize: var url = “http://localhost:8080/CalculatorWS/CalculatorWS”; readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Initialize: xmlHttp.open("POST",url,true); readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Initialize: var soapReq = “<?xml version=\"1.0\" encoding=\"UTF-8\"?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Header/><S:Body><ns2:add xmlns:ns2=\"http://calculator.me.org/\"><x>" + document.getElementById("input1").value + "</x><y>" + document.getElementById("input2").value + "</y></ns2:add></S:Body></S:Envelope>”; readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Send to the server: xmlHttp.send(soapReq); readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Receive from server: function stateChanged(){ if (xmlHttp.readyState==4){ document.getElementById("table").innerHTML=xmlHttp.responseText; } } readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest • Receive from server: function respAjax(){ if(xmlHttp.readyState == 4{ document.getElementById(“result”).innerHTML = ... } readyState values: 0 – uninitialized 1 – loading 2 – loaded 3 – interactive 4 - complete
XMLHttpRequest <body> <h1>Ajax Web Service</h1> <input id="input1" size = "5" type="text" /> <input id="input2" size = "5" type="text" /> <input type="button" value="click me" onClick="sendAjax()" /> <div id="result"> </div> </body>
XMLHttpRequest <body> <h1>Ajax Web Service</h1> <input id="input1" size = "5" type="text" /> <input id="input2" size = "5" type="text" /> <input type="button" value="click me" onClick="sendAjax()" /> <div id="result"> </div> </body>
XMLHttpRequest <input id="input1" size = "5" type="text" /> <input id="input2" size = "5" type="text" /> var soapReq = “<?xml version=\"1.0\" encoding=\"UTF-8\"?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Header/><S:Body><ns2:add xmlns:ns2=\"http://calculator.me.org/\"><x>" + document.getElementById("input1").value + "</x><y>" + document.getElementById("input2").value + "</y></ns2:add></S:Body></S:Envelope>”;
XMLHttpRequest <input id="input1" size = "5" type="text" /> <input id="input2" size = "5" type="text" /> var soapReq = “<?xml version=\"1.0\" encoding=\"UTF-8\"?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Header/><S:Body><ns2:add xmlns:ns2=\"http://calculator.me.org/\"><x>" + document.getElementById("input1").value + "</x><y>" + document.getElementById("input2").value + "</y></ns2:add></S:Body></S:Envelope>”;
XMLHttpRequest <body> <h1>Ajax Web Service</h1> <input id="input1" size = "5" type="text" /> <input id="input2" size = "5" type="text" /> <input type="button" value="click me" onClick="sendAjax()" /> <div id="result"> </div> </body>
XMLHttpRequest • Receive from server: function respAjax(){ if(xmlHttp.readyState == 4{ document.getElementById(“result”).innerHTML = ... }
bob.innerHTML = xmlHttp.responseText <html> <body> <div id=‘bob’> <img src=‘fish.jpg’ /> </div> </body> </html> responseText <img src=‘dog.jpg’ />
bob.innerHTML = xmlHttp.responseText <html> <body> <div id=‘bob’> </div> </body> </html> responseText <img src=‘dog.jpg’ />
XMLHttpRequest • xmlHttp.responseXML <?xml version='1.0' encoding='UTF-8'?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:addResponse xmlns:ns2="http://calculator.me.org/"> <return>10</return> </ns2:addResponse> </S:Body> </S:Envelope>
XMLHttpRequest • xmlHttp.responseXML <?xml version='1.0' encoding='UTF-8'?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:addResponse xmlns:ns2="http://calculator.me.org/"> <return>10</return> </ns2:addResponse> </S:Body> </S:Envelope>
XMLHttpRequest • xmlHttp.responseXML document.getElementById(“result”).innerHTML = ... xmlHttp.responseXML.getElementsByTagName(“return”)
XMLHttpRequest • xmlHttp.responseXML document.getElementById(“result”).innerHTML = ... xmlHttp.responseXML.getElementsByTagName(“return”)[0]
XMLHttpRequest • xmlHttp.responseXML document.getElementById(“result”).innerHTML = ... xmlHttp.responseXML.getElementsByTagName(“return”)[0].childNodes[0]
XMLHttpRequest • xmlHttp.responseXML document.getElementById(“result”).innerHTML = ... xmlHttp.responseXML.getElementsByTagName(“return”)[0].childNodes[0].nodeValue
XMLHttpRequest function respAjax(){ if(xmlHttp.readyState == 4){ document.getElementById("result").innerHTML = "<h3>Result is: " + xmlHttp.responseXML.getElementsByTagName("return")[0].childNodes[0].nodeValue + "</h3>"; }
Finally! • Script tag in the head section <head> <script type=“text/javascript”> var xmlHttp; function sendAjax(){... function respAjax(){... </script> </head>