「JavaScript入門」XMLHttpRequest でajaxのget方法を実装するコード

2017年12月13日

JSコード
var makeXMLHttpRequest = function(url) {
var httpRequest;

if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml’);
}
} else if (window.ActiveXObject) {
// IE
try {
httpRequest = new ActiveXObject(“Msxml2.XMLHTTP");
} catch (e) {
try {
httpRequest = new ActiveXObject(“Microsoft.XMLHTTP");
} catch (e) {
}
}
}

if (!httpRequest) {
console.log('Cannot create XMLHTTP instance’);
return false;
}

httpRequest.onreadystatechange = function() {
processResponse(httpRequest);
};
httpRequest.open('GET’, url, true);
httpRequest.send(");
};

var processResponse = function(httpRequest) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
doSomething(httpRequest);
} else {
// エラー処理
}
}
};

JavaScript

Posted by arkgame