1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
<html>
<head>
<title>XmlHttpRequest - AJAX</title>
<script language="javascript">
function getHTTPObject() {
var xhr_object = null;
if(window.XMLHttpRequest) // Firefox
xhr_object = new XMLHttpRequest();
else if(window.ActiveXObject) // Internet Explorer
xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
return;
}
xhr_object.open("GET", "traitement.php", true);
//parametres à soumettre
data = null;
xhr_object.onreadystatechange = function() {
//traitement
if(xhr_object.readyState == 4) {
if (xhr_object.status == 200) {
var xmlDocument = xhr_object.responseXML;alert(xmlDocument);
//var city = xmlDocument.getElementsByTagName('city')[0].firstChild.data;
//var state = xmlDocument.getElementsByTagName('state')[0].firstChild.data;
}else{
alert(xhr_object.statusText);
}
document.getElementById("divResult").innerHTML = city + ' - ' + state;
}
}
//si on utilise la methode POST
//xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr_object.send(data);
}
</script>
</head>
<body>
<form action="post">
<input type="text" id="txtName" />
<a href="#" id="btnSubmit" onClick="getHTTPObject()">Go</a>
</form>
<br><br>
<div id="divResult"></div>
</body>
</html> |