X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F06_net%2F01_XNetXHR.js;h=f4883506541aa5ddfeb6a23cc90b417c9008d44a;hb=babda23632a3176d9fe546fbab6f79c1fef0e3d4;hp=b311ce73b609f79d2fbb041aa393b61c7b9104c3;hpb=94c39d10a21853703c90cb09b1e82bd7a2d8923e;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/06_net/01_XNetXHR.js b/0.6.x/js/06_net/01_XNetXHR.js index b311ce7..f488350 100644 --- a/0.6.x/js/06_net/01_XNetXHR.js +++ b/0.6.x/js/06_net/01_XNetXHR.js @@ -18,6 +18,17 @@ new ActiveXObject( 'Msxml2.XMLHTTP' ); // バージョンを省略すると new ActiveXObject( 'Msxml2.XMLHTTP.4.0' ); // バージョン4.0 は bugfix が行われないので、3.0 か 6.0 を指定すべき new ActiveXObject( 'Msxml2.XMLHTTP.5.0' ); // バージョン5.0 は bugfix が行われないので、3.0 か 6.0 を指定すべき +[IE][Javascript][Json] IE+JsonではまったAdd Star +http://d.hatena.ne.jp/khiker/20081026/javascript_json +> AddCharset utf-8 json +> AddType text/javascript json + +JavaScriptでJSONをeval +http://d.hatena.ne.jp/sshi/20060904/p1 + +itozyun 2014-10-30 20:55:41 +basic 認証のかかったhtml を表示して、そのjsが xhr をすると Android1.6 では 401 error が返る。Android 2.3 では解決している。 +Android1.6- の XHR で 401 エラーが返った場合は、iframe に xml を表示させてその内容を取ればサーバ側の対応無しでいけるかも? */ var X_Net_XHR_W3C = ( !X_UA.IE7 || !X_URL_IS_LOCAL ) && window[ 'XMLHttpRequest' ] && new XMLHttpRequest(), X_Net_XHR_X_DOMAIN = window[ 'XDomainRequest' ] && new XDomainRequest(), @@ -31,7 +42,7 @@ var X_Net_XHR_W3C = ( !X_UA.IE7 || !X_URL_IS_LOCAL ) && window[ 'XMLHttpReq 'for(;i<5;){', 'try{', 'return[v[++i],new ActiveXObject(n[i])]', - '}catch(e){return false}', + '}catch(e){}', '}' ].join( '' ) ) )(); @@ -45,7 +56,8 @@ X.Net.XHR = { W3C : !!X_Net_XHR_W3C, X_DOMAIN : !!X_Net_XHR_X_DOMAIN, - + + // ie7 ではローカルリソースには ActiveX の XHR を使う ACTIVE_X : !!X_Net_XHR_ACTIVE_X, /* @@ -55,9 +67,6 @@ X.Net.XHR = { * Flash から JavaScript にアクセスする(3+) */ FLASH : false, - - -// ie7 ではローカルリソースには ActiveX の XHR を使う // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest // Progress Events Chrome7, firefox3.5, ie10, opera12, Safari?, Chrome for Android 0.16 @@ -85,6 +94,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ _type : '', _busy : false, _canceled : false, + _error : false, _percent : 0, _timerID : 0, @@ -138,7 +148,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ break; case 'json' : case 'moz-json' : - raw.responseType = this._type; + raw.responseType = X_UA.Gecko ? this._type : ''; // Iron 37 でエラー break; case 'document' : case 'xml' : @@ -159,12 +169,13 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ // raw.overrideMimeType() if( !X_Net_XHR_ACTIVE_X && X.Type.isFunction( raw.setRequestHeader ) ){ for( p in headers ){ + if( X_EMPTY_OBJECT[ p ] ) continue; raw.setRequestHeader( p, headers[ p ] ); // Opera8.01+, MSXML3+ }; }; if( raw.timeout !== undefined ){ - raw.timeout = timeout; //Firefox33 でエラー,,, + raw.timeout = timeout; } else { this._timerID = X.Timer.once( timeout, this, this.onTimeout ); }; @@ -172,13 +183,24 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ // send 前にフラグを立てる,回線が早いと raw.send() 内で onload -> _busy = false ののち、 _busy = true するため。 this._busy = true; - + + raw.send( postbody || '' ); + //this._timerID = X.Timer.once( 16, this, this._lazySend, [ postbody || '', timeout ] ); + }, + /* + // send() 内で onload するケースがあり、そのときはイベントリスナが間に合わないので、タイマーをかませる。 + _lazySend : function( postbody, timeout ){ + if( this._rawObject.timeout === undefined ){ + this._timerID = X.Timer.once( timeout, this, this.onTimeout ); + } else { + this._timerID = 0; + }; // http://allabout.co.jp/gm/gc/24097/#1 // sendをonreadystatechangeの前に記述すると、ieでは動作しなくなります、、、。 // konquerorでエラーが発生するのでここでは、とりあえず、send('') としました。 - raw.send( postbody || '' ); - }, + this._rawObject.send( postbody ); + }, */ cancel : function(){ /* X.Net.XHR.CANCELABLE && */ this._rawObject.abort && this._rawObject.abort(); @@ -195,8 +217,14 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ // domes.lingua.heliohost.org/dom-intro/load-save2.html // 規定上は open() を呼び出すと XMLHttpRequest オブジェクトが未送信状態に戻りますが、 // Opera 10.10、Safari 4.1 では、同一オリジン制限に違反した XMLHttpRequest オブジェクトは再度 open() しても未送信状態に戻りません。 - if( X_UA.Opera || X_UA.Webkit ){ - + + // Timeout した Gecko の xhr.response に触るとエラー?? + if( X_UA.Opera || X_UA.Webkit || X_UA.Gecko ){ + if( this._error ){ + X_EventDispatcher_toggleAllEvents( this, false ); + this._rawObject = new XMLHttpRequest(); + X_EventDispatcher_toggleAllEvents( this, true ); + }; }; // XMLHttpRequest の使い方 @@ -205,7 +233,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ this._rawObject.abort && this._rawObject.abort(); this._method = this._type = ''; - this._canceled = this._busy = false; + this._canceled = this._busy = this._error = false; this._timerID && X.Timer.remove( this._timerID ); this._percent = this._timerID = 0; }, @@ -256,7 +284,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ case 'load' : if( !live ) return this.reset(); - if( this._percent === 100 ) return; + if( !this._busy ) return; this._percent = 100; @@ -286,13 +314,18 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ break; case 'json' : case 'moz-json' : - data = raw[ 'response' ] || raw[ 'responseText' ]; // とりあえず + data = raw[ 'response' ] || raw[ 'responseText' ]; + // eval() を使っているけど JSON の無いブラウザは XDomain な XHR はできないのでよしとする。 + // XDomain な XHR の際は Flash 等で代替し、その中に Json parser も組み込む。 + // http://d.hatena.ne.jp/sshi/20060904/p1 + if( !X.Type.isObject( data ) ) data = window.JSON ? JSON.parse( data ) : eval( '(' + data + ')' ); break; case 'document' : case 'xml' : case 'html' : case 'htm' : - data = raw[ 'responseXML' ]; + // svg, vml, xaml, xul, mxml ?? + data = raw[ 'responseXML' ] || raw[ 'response' ] || raw[ 'responseText' ]; // とりあえず break; case 'blob' : case 'arraybuffer' : @@ -300,9 +333,9 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ break; }; - this.asyncDispatch( { type : X.Event.SUCCESS, status : status || 200, data : data } ); + this.asyncDispatch( 32, { type : X.Event.SUCCESS, status : status || 200, data : data } ); } else { - live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status || 0, percent : 100 } ); + live && this.asyncDispatch( 32, { type : X.Event.ERROR, status : raw.status || 0, percent : 100 } ); }; break; @@ -315,12 +348,14 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ case 'error' : //console.dir( e ); - this._busy = false; - live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status } ); + this._busy = false; + this._error = X_UA.Opera || X_UA.Webkit; + live && this.asyncDispatch( 32, { type : X.Event.ERROR, status : raw.status } ); break; case 'timeout' : // Gecko 12.0 https://developer.mozilla.org/ja/docs/XMLHttpRequest/Synchronous_and_Asynchronous_Requests - this._busy = false; + this._busy = false; + this._error = !!X_UA.Gecko; this.asyncDispatch( X.Event.TIMEOUT ); break; }; @@ -366,8 +401,8 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ X_NET_XHRWrapper.listen( [ 'load', 'readystatechange', 'error', 'timeout' ] ); //, 'abort' }; - if( X.Net.XHR.UL_PROGRESS ){ - X_NET_XHRWrapper._rawObject.upload.addEventListener( 'progress', X.Net.XHR.xhr.onUploadProgress ); + if( X_NET_XHRWrapper.onUploadProgress ){ + X_NET_XHRWrapper._rawObject.upload.addEventListener( 'progress', X_NET_XHRWrapper.onUploadProgress ); }; };