AJax非同期通信
■ HTTPRequestオブジェクトを生成から、HTTP受信して情報を表示するまで
※サーバーにtest.xmlというファイルを配置しています。
var httpRequest;
//I.E.の場合
if(window.ActiveXObject){
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
if(!httpRequest && window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
}
httpRequest.open('GET', './test.xml');
//ヘッダーを追加する。
httpRequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
//受信後の処理
httpRequest.onreadystatechange = function(){
//HTTPレスポンスが200 OKの場合のみ処理
if(httpRequest.readyState==4 && httpRequest.status == 200){
//指定したHTTPヘッダーを取得
//alert(httpRequest.getResponseHeader("Content-Type"));
//HTTPヘッダーを全部取得
//alert(httpRequest.getAllResponseHeaders());
//HTTPボディ内容をテキストとして表示
alert(httpRequest.responseText);
}
}
//リクエスト送信
httpRequest.send(null);