Created
December 15, 2011 06:33
-
-
Save froop/1480073 to your computer and use it in GitHub Desktop.
[JavaScript] Ajax簡易ユーティリティ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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