@@ -209,6 +209,7 @@ window.XHRCreep = (function(){
209209 } ;
210210} ) ( ) ;
211211
212+
212213window . jSQL . xhrCache = ( function ( callback ) {
213214 jSQL . load ( function ( ) {
214215
@@ -278,16 +279,19 @@ window.jSQL.xhrCache = (function(callback){
278279 var _this = this ;
279280 if ( proceed ) {
280281
281- // make the request and intercept the response
282- var orsc = this . onreadystatechange ;
283- this . onreadystatechange = function ( ) {
282+ this . _xhr . onreadystatechange = function ( ) {
283+ _this . getMockProperties ( ) ;
284284
285285 if ( _this . readyState === 4 && _this . status === 200 ) {
286286 // ony cache raw text and json responses
287287 if ( [ '' , 'json' , 'text' ] . indexOf ( _this . _xhr . responseType ) > - 1 ) {
288- var response = _this . _xhr . responseText ;
289- if ( ( _this . _xhr . getResponseHeader ( "content-type" ) || "" ) . indexOf ( 'json' ) > - 1 ) {
290- response = "" + JSON . stringify ( _this . _xhr . response ) ;
288+ var response = _this . _xhr . response ;
289+ var respType = _this . _xhr . responseType ;
290+ if ( "object" === typeof response || ( _this . _xhr . getResponseHeader ( "content-type" ) || "" ) . indexOf ( 'json' ) > - 1 ) {
291+
292+ if ( "object" === typeof response ) response = "" + JSON . stringify ( _this . _xhr . response ) ;
293+ respType = 'json' ;
294+
291295 }
292296
293297 var responseText = "" ;
@@ -301,10 +305,10 @@ window.jSQL.xhrCache = (function(callback){
301305 responseURL : _this . _xhr . responseURL ,
302306 responseXML : responseXML ,
303307 status : _this . _xhr . status ,
304- responseType : _this . _xhr . responseType ,
308+ responseType : respType ,
305309 responseText : responseText ,
306- statusText : _this . _xhr . statusText } ;
307-
310+ statusText : _this . _xhr . statusText ,
311+ headers : _this . _xhr . getAllResponseHeaders ( ) } ;
308312
309313 var id = jSQL . query ( "select * from jSQLCache" ) . execute ( ) . fetchAll ( "ARRAY" ) . length ;
310314 query = jSQL . query ( "insert into jSQLCache values (?, ?, ?, ?, ?, ?, ?, ?, ?)" ) ;
@@ -317,34 +321,62 @@ window.jSQL.xhrCache = (function(callback){
317321 console . log ( "jSQL.xhrCache: Caching AJAX response" ) ;
318322 }
319323 }
320-
324+
321325 // call the original onreadystatechange
322- orsc . apply ( _this , arguments ) ;
326+ _this . onreadystatechange . apply ( _this . _xhr , arguments ) ;
327+
323328 } ;
324329 // send the request
325330 this . _xhr . send . apply ( this . _xhr , arguments ) ;
326331
327332 } else {
328-
329- this . response = results [ 0 ] . response . response ;
330- this . readyState = results [ 0 ] . response . readyState ;
331- this . responseURL = results [ 0 ] . response . responseURL ;
332- this . responseXML = results [ 0 ] . response . responseXML ;
333- this . status = results [ 0 ] . response . status ;
334- this . responseType = results [ 0 ] . response . responseType ;
335- this . responseText = results [ 0 ] . response . responseText ;
336- this . statusText = results [ 0 ] . response . statusText ;
337- if ( jSQL . xhrCache . logging ) {
338- console . groupCollapsed ( "jSQL.xhrCache: " + this . _METHOD . toUpperCase ( ) + ": " + this . _URL ) ;
339- console . log ( "METHOD: " , this . _METHOD ) ;
340- console . log ( "POSTDATA: " , this . _POST_DATA ) ;
341- console . log ( "RESPONSE TYPE: " , this . responseType ) ;
342- console . log ( "RESPONSE: " , this . response ) ;
343- console . log ( "Cached about " + minutes_old + " minutes ago." ) ;
344- console . groupEnd ( ) ;
345- }
346- this . onreadystatechange . apply ( this , arguments ) ;
347- this . onload . apply ( this , arguments ) ;
333+
334+ setTimeout ( function ( ) {
335+
336+ _this . response = results [ 0 ] . response . responseType === 'json' ?
337+ JSON . parse ( results [ 0 ] . response . response ) : results [ 0 ] . response . response ;
338+
339+ _this . readyState = results [ 0 ] . response . readyState ;
340+ _this . responseURL = results [ 0 ] . response . responseURL ;
341+ _this . responseXML = results [ 0 ] . response . responseXML ;
342+ _this . status = results [ 0 ] . response . status ;
343+ _this . responseType = results [ 0 ] . response . responseType ;
344+ _this . responseText = results [ 0 ] . response . responseText ;
345+ _this . statusText = results [ 0 ] . response . statusText ;
346+
347+ // override the getResponseHeader
348+ _this . getResponseHeader = function ( name ) {
349+ name = name . toUpperCase ( ) . trim ( ) ;
350+ var s = results [ 0 ] . response . headers . split ( "\n" ) ;
351+ for ( var i = s . length ; i -- ; ) {
352+ if ( s [ i ] === "" ) continue ;
353+ var parts = s [ i ] . split ( ":" ) ; console . log ( parts ) ;
354+ var n = ( parts . shift ( ) + "" ) . toUpperCase ( ) . trim ( ) ;
355+ if ( n === name ) return parts . join ( ":" ) . trim ( ) ;
356+ }
357+ return null ;
358+ } ;
359+
360+ _this . getAllResponseHeaders = function ( ) {
361+ return results [ 0 ] . response . headers ;
362+ } ;
363+
364+
365+ if ( jSQL . xhrCache . logging ) {
366+ console . groupCollapsed ( "jSQL.xhrCache: " + _this . _METHOD . toUpperCase ( ) + ": " + _this . _URL ) ;
367+ console . log ( "METHOD: " , _this . _METHOD ) ;
368+ console . log ( "POSTDATA: " , _this . _POST_DATA ) ;
369+ console . log ( "RESPONSE TYPE: " , _this . responseType ) ;
370+ console . log ( "RESPONSE: " , _this . response ) ;
371+ console . log ( "Cached about " + minutes_old + " minutes ago." ) ;
372+ console . groupEnd ( ) ;
373+ }
374+
375+ _this . onreadystatechange . apply ( _this , arguments ) ;
376+ _this . onload . apply ( _this , arguments ) ;
377+ } , 50 ) ;
378+
379+
348380
349381 }
350382 } ;
0 commit comments