API Docs for: 最后更新日期:2014年8月25日
Google搜索   
Show:

File: net/LAjax.js

/** @language japanese
 * AJAX アプリケーション。
 * @class LAjax
 * @constructor
 * @since 1.7.1
 * @public
 */
var LAjax = (function () {
	function LAjax () {
		this.responseType = null;
	}
	LAjax.prototype = {
		TEXT : "text",
		ARRAY_BUFFER : "arraybuffer",
		BLOB : "blob",
		/** @language japanese
		 * HTTP(GET)通信でページを読み込みます。
		 * @method get
		 * @param {String} url 必須。読み込むHTMLページのURL
		 * @param {Json Object} data パラメータ(キーと値の組み合わせ)
		 * @param {function} oncomplete 通信成功時のコールバック関数。
		 * @param {function} onerror 通信失敗時のコールバック関数。
		 * @example
		 * 	LInit(1000/50,"legend",800,450,main);
		 * 	var label;
		 * 	function main(){
		 * 		label = new LTextField();
		 * 		addChild(label);
		 * 		label.x = label.y = 50;
		 * 		label.text = "LAjax.get ......";
		 * 		LAjax.get("test.txt",{},success);
		 * 	}
		 * 	function success (data) {
		 * 		label.text = data;
		 * 	}
		 * @examplelink <p><a href="../../../api/LAjax/get.html" target="_blank">実際のサンプルを見る</a></p>
		 * @public
		 * @since 1.7.1
		 */
		get : function (url, data, oncomplete, onerror) {
			this.getRequest("GET", url, data, oncomplete, onerror);
		},
		/** @language japanese
		 * HTTP(POST)通信でページを読み込みます。
		 * @method post
		 * @param {String} url 必須。読み込むHTMLページのURL
		 * @param {Json Object} data パラメータ(キーと値の組み合わせ)
		 * @param {function} oncomplete 通信成功時のコールバック関数。
		 * @param {function} onerror 通信失敗時のコールバック関数。
		 * @example
		 * 	LInit(1000/50,"legend",800,450,main);
		 * 	var label;
		 * 	function main(){
		 * 		label = new LTextField();
		 * 		addChild(label);
		 * 		label.x = label.y = 50;
		 * 		label.text = "LAjax.post ......";
		 * 		LAjax.post("test.txt",{},success);
		 * 	}
		 * 	function success (data) {
		 * 		label.text = data;
		 * 	}
		 * @examplelink <p><a href="../../../api/LAjax/post.html" target="_blank">実際のサンプルを見る</a></p>
		 * @public
		 * @since 1.7.1
		 */
		post : function (url, data, oncomplete, onerror) {
			this.getRequest("POST", url, data, oncomplete, onerror);
		},
		getRequest : function (t, url, d, oncomplete, err) {
			var s = this, k, data = "", a = "";
			s.err = err;
			var ajax = s.getHttp();
			if (!ajax) {
				return;
			}
			if (d) {
				for (k in d) {
					data += (a + k + "=" + d[k]);
					a = "&";	
				}
			}
			if (t.toLowerCase() == "get") {
				url += ((url.indexOf('?') >= 0 ? '&' : '?') + data);
				data = null;
			}
			ajax.open(t, url, true);
			if (s.responseType) {
				ajax.responseType = s.responseType;
				s.responseType = s.TEXT;
			}
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.onreadystatechange = function () {
				if (ajax.readyState == 4) {
					if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
						if (oncomplete) {
							if (ajax.responseType == s.ARRAY_BUFFER || ajax.responseType == s.BLOB) {
								oncomplete(ajax.response);
							} else if (ajax.responseText.length > 0) {
								oncomplete(ajax.responseText);
							} else {
								oncomplete(null);
							}
						}
					} else {
						if (err) {
							err(ajax);
						}
					}
		 		}
			};
			ajax.send(data);
		},
		getHttp : function () {
			if (typeof XMLHttpRequest != UNDEFINED) {
				return new XMLHttpRequest();
			}  
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					if (!this.err) {
						this.err(e);
					}
				}
			}
			return false;
		}
	};
	return new LAjax();
})();