1060: onStateChange: function() {
1061: var readyState = this.transport.readyState;
1062: if (readyState > 1 && !((readyState == 4) && this._complete))
1063: this.respondToReadyState(this.transport.readyState);
1064: },
1065:
1060行目からはonStateChange()メソッドです。
XHRが保持するreadyState変数を取得し,
1066: setRequestHeaders: function() {
1067: var headers = {
1068: 'X-Requested-With': 'XMLHttpRequest',
1069: 'X-Prototype-Version': Prototype.Version,
1070: 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
1071: };
1072:
1073: if (this.method == 'post') {
1074: headers['Content-type'] = this.options.contentType +
1075: (this.options.encoding ? '; charset=' + this.options.encoding : '');
1076:
1077: /* Force "Connection: close" for older Mozilla browsers to work
1078: * around a bug where XMLHttpRequest sends an incorrect
1079: * Content-length header. See Mozilla Bugzilla #246651.
1080: */
1081: if (this.transport.overrideMimeType &&
1082: (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
1083: headers['Connection'] = 'close';
1084: }
1085:
1086: // user-defined headers
1087: if (typeof this.options.requestHeaders == 'object') {
1088: var extras = this.options.requestHeaders;
1089:
1090: if (typeof extras.push == 'function')
1091: for (var i = 0, length = extras.length; i < length; i += 2)
1092: headers[extras[i]] = extras[i+1];
1093: else
1094: $H(extras).each(function(pair) { headers[pair.key] = pair.value });
1095: }
1096:
1097: for (var name in headers)
1098: this.transport.setRequestHeader(name, headers[name]);
1099: },
1100:
1066行目からはsetRequestHeaders()メソッドです。これは他の場所からも利用しうるから単独の関数にした,
まず,
次に,
1077行目からは,
1086行目からはoptions.
ここではHash形式のオブジェクトか配列が想定されているため,
いったんextrasという変数に代入した後に,
最後に1097行目から,
1101: success: function() {
1102: return !this.transport.status
1103: || (this.transport.status >= 200 && this.transport.status < 300);
1104: },
1105:
1101行目からはsuccess()メソッドです。
XHRのstatusプロパティが偽なら偽を返し,
statusプロパティが利用できるのは,
statusにまだ値が入っていなければ偽を返します。
1106: respondToReadyState: function(readyState) {
1107: var state = Ajax.Request.Events[readyState];
1108: var transport = this.transport, json = this.evalJSON();
1109:
1110: if (state == 'Complete') {
1111: try {
1112: this._complete = true;
1113: (this.options['on' + this.transport.status]
1114: || this.options['on' + (this.success() ? 'Success' : 'Failure')]
1115: || Prototype.emptyFunction)(transport, json);
1116: } catch (e) {
1117: this.dispatchException(e);
1118: }
1119:
1120: var contentType = this.getHeader('Content-type');
1121: if (contentType && contentType.strip().
1122: match(/^(text|application)\/(x-)?(java|ecma)script(;.*)?$/i))
1123: this.evalResponse();
1124: }
1125:
1126: try {
1127: (this.options['on' + state] || Prototype.emptyFunction)(transport, json);
1128: Ajax.Responders.dispatch('on' + state, this, transport, json);
1129: } catch (e) {
1130: this.dispatchException(e);
1131: }
1132:
1133: if (state == 'Complete') {
1134: // avoid memory leak in MSIE: clean up
1135: this.transport.onreadystatechange = Prototype.emptyFunction;
1136: }
1137: },
1138:
1106行目からはrespondToReadyState()メソッドです。
readyStateの各状態に対応して,
まず,
jsonという変数に,
1110行目からはstateが'Complete'の時の処理です。XHRでは,
this._completeフラグをtrueに設定し,
ここでは'on200'などのステータスコードに関連付けられたイベント処理関数がoptionsに登録されていればそれを優先し,
1120行目からは,
- application/
ecmascript - application/
javascript - application/
x-ecmascript - application/
x-javascript - text/
ecmascript - text/
javascript - text/
x-ecmascript - text/
x-javascript
ならevalResponse()メソッドを使ってレスポンスボディをJSONとみなしeval()します。
1126行目からは,
最後に,