Skip to content

Instantly share code, notes, and snippets.

@froop
Created December 15, 2011 06:33
Show Gist options
  • Select an option

  • Save froop/1480073 to your computer and use it in GitHub Desktop.

Select an option

Save froop/1480073 to your computer and use it in GitHub Desktop.
[JavaScript] Ajax簡易ユーティリティ
/**
* Ajax用オブジェクト(XMLHttpRequest)生成.
*/
function createAjaxRequest() {
var req;
try {
req = new XMLHttpRequest();
} catch (e) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return req;
}
/**
* 非同期リクエスト送信.
* @param url リクエストURL
* @param sending 送信中テキスト
* @param error エラー時テキスト
* @param resFunc 応答処理関数 (function (responseText) {...})
*/
function asyncSend(url, sending, error, resFunc) {
var req = createAjaxRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
resFunc(req.responseText);
} else {
resFunc(error);
}
} else {
resFunc(sending);
}
};
req.open("GET", url, true);
req.send(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment