1- module . exports = function ( Vue ) {
1+ /**
2+ * Service for sending network requests.
3+ */
24
3- var _ = require ( './util' ) ( Vue ) ;
4- var Promise = require ( './promise' ) ;
5- var jsonType = { 'Content-Type' : 'application/json;charset=utf-8' } ;
5+ var _ = require ( './lib/util' ) ;
6+ var xhr = require ( './lib/xhr' ) ;
7+ var jsonp = require ( './lib/jsonp' ) ;
8+ var jsonType = { 'Content-Type' : 'application/json;charset=utf-8' } ;
69
7- /**
8- * Http provides a service for sending XMLHttpRequests.
9- */
10+ module . exports = function ( Vue ) {
1011
11- function Http ( url , options ) {
12+ function Http ( url , options ) {
1213
1314 var self = this , headers , promise ;
1415
@@ -30,7 +31,7 @@ module.exports = function (Vue) {
3031
3132 if ( _ . isPlainObject ( options . data ) && / ^ ( g e t | j s o n p ) $ / i. test ( options . method ) ) {
3233 _ . extend ( options . params , options . data ) ;
33- options . data = '' ;
34+ delete options . data ;
3435 }
3536
3637 promise = ( options . method . toLowerCase ( ) == 'jsonp' ? jsonp : xhr ) . call ( this , this . $url || Vue . url , options ) ;
@@ -77,110 +78,6 @@ module.exports = function (Vue) {
7778 return promise ;
7879 }
7980
80- function xhr ( url , options ) {
81-
82- var request = new XMLHttpRequest ( ) ;
83-
84- if ( _ . isFunction ( options . beforeSend ) ) {
85- options . beforeSend ( request , options ) ;
86- }
87-
88- if ( options . emulateHTTP && / ^ ( p u t | p a t c h | d e l e t e ) $ / i. test ( options . method ) ) {
89- options . headers [ 'X-HTTP-Method-Override' ] = options . method ;
90- options . method = 'post' ;
91- }
92-
93- if ( options . emulateJSON && _ . isPlainObject ( options . data ) ) {
94- options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
95- options . data = url . params ( options . data ) ;
96- }
97-
98- if ( _ . isObject ( options . data ) && / F o r m D a t a / i. test ( options . data . toString ( ) ) ) {
99- delete options . headers [ 'Content-Type' ] ;
100- }
101-
102- if ( _ . isPlainObject ( options . data ) ) {
103- options . data = JSON . stringify ( options . data ) ;
104- }
105-
106- var promise = new Promise ( function ( resolve , reject ) {
107-
108- request . open ( options . method , url ( options ) , true ) ;
109-
110- _ . each ( options . headers , function ( value , header ) {
111- request . setRequestHeader ( header , value ) ;
112- } ) ;
113-
114- request . onreadystatechange = function ( ) {
115-
116- if ( this . readyState === 4 ) {
117-
118- if ( this . status >= 200 && this . status < 300 ) {
119- resolve ( this ) ;
120- } else {
121- reject ( this ) ;
122- }
123- }
124- } ;
125-
126- request . send ( options . data ) ;
127- } ) ;
128-
129- _ . extend ( promise , {
130-
131- abort : function ( ) {
132- request . abort ( ) ;
133- }
134-
135- } ) ;
136-
137- return promise ;
138- }
139-
140- function jsonp ( url , options ) {
141-
142- var callback = '_jsonp' + Math . random ( ) . toString ( 36 ) . substr ( 2 ) , script , result ;
143-
144- options . params [ options . jsonp ] = callback ;
145-
146- if ( _ . isFunction ( options . beforeSend ) ) {
147- options . beforeSend ( { } , options ) ;
148- }
149-
150- var promise = new Promise ( function ( resolve , reject ) {
151-
152- script = document . createElement ( 'script' ) ;
153- script . src = url ( options . url , options . params ) ;
154- script . type = 'text/javascript' ;
155- script . async = true ;
156-
157- window [ callback ] = function ( data ) {
158- result = data ;
159- } ;
160-
161- var handler = function ( event ) {
162-
163- delete window [ callback ] ;
164- document . body . removeChild ( script ) ;
165-
166- if ( event . type === 'load' && ! result ) {
167- event . type = 'error' ;
168- }
169-
170- var text = result ? result : event . type , status = event . type === 'error' ? 404 : 200 ;
171-
172- ( status === 200 ? resolve : reject ) ( { responseText : text , status : status } ) ;
173- } ;
174-
175- script . onload = handler ;
176- script . onerror = handler ;
177-
178- document . body . appendChild ( script ) ;
179- } ) ;
180-
181- return promise ;
182- }
183-
18481 function transformResponse ( response ) {
18582
18683 try {
0 commit comments