22// Browser
33//////////////////////////////
44
5- function Browser ( location , document ) {
5+ function Browser ( location , document , head ) {
66 this . delay = 50 ;
77 this . expectedUrl = location . href ;
88 this . urlListeners = [ ] ;
@@ -21,8 +21,9 @@ function Browser(location, document) {
2121 } ;
2222
2323 this . location = location ;
24- this . document = jqLite ( document ) ;
25- this . body = jqLite ( document . body ) ;
24+ this . document = document ;
25+ this . head = head ;
26+ this . idCounter = 0 ;
2627}
2728
2829Browser . prototype = {
@@ -58,21 +59,34 @@ Browser.prototype = {
5859 callback = post ;
5960 post = null ;
6061 }
61- var xhr = new this . XHR ( ) ,
62- self = this ;
63- xhr . open ( method , url , true ) ;
64- this . outstandingRequests . count ++ ;
65- xhr . onreadystatechange = function ( ) {
66- if ( xhr . readyState == 4 ) {
67- try {
68- callback ( xhr . status || 200 , xhr . responseText ) ;
69- } finally {
70- self . outstandingRequests . count -- ;
71- self . processRequestCallbacks ( ) ;
62+ if ( lowercase ( method ) == 'json' ) {
63+ var callbackId = "angular_" + Math . random ( ) + '_' + ( this . idCounter ++ ) ;
64+ callbackId = callbackId . replace ( / \d \. / , '' ) ;
65+ var script = this . document [ 0 ] . createElement ( 'script' ) ;
66+ script . type = 'text/javascript' ;
67+ script . src = url . replace ( 'JSON_CALLBACK' , callbackId ) ;
68+ this . head . append ( script ) ;
69+ window [ callbackId ] = function ( data ) {
70+ delete window [ callbackId ] ;
71+ callback ( 200 , data ) ;
72+ } ;
73+ } else {
74+ var xhr = new this . XHR ( ) ,
75+ self = this ;
76+ xhr . open ( method , url , true ) ;
77+ this . outstandingRequests . count ++ ;
78+ xhr . onreadystatechange = function ( ) {
79+ if ( xhr . readyState == 4 ) {
80+ try {
81+ callback ( xhr . status || 200 , xhr . responseText ) ;
82+ } finally {
83+ self . outstandingRequests . count -- ;
84+ self . processRequestCallbacks ( ) ;
85+ }
7286 }
73- }
74- } ;
75- xhr . send ( post || '' ) ;
87+ } ;
88+ xhr . send ( post || '' ) ;
89+ }
7690 } ,
7791
7892 processRequestCallbacks : function ( ) {
0 commit comments