@@ -94,8 +94,10 @@ export class Ajax<T = unknown> {
9494
9595 then ( resolve : ( data : T ) => void , reject ?: ( error : Error ) => void ) {
9696 if ( this . completed ) {
97- if ( reject && this . error ) {
98- reject ( this . error ) ;
97+ if ( this . error ) {
98+ if ( reject ) {
99+ reject ( this . error ) ;
100+ }
99101 } else {
100102 resolve ( this . data ) ;
101103 }
@@ -151,6 +153,10 @@ export class Ajax<T = unknown> {
151153 accepts,
152154 dataType,
153155 timeout,
156+ jsonParser,
157+ traditional,
158+ convert,
159+ throws,
154160 dataFilter,
155161 beforeSend,
156162 success,
@@ -201,6 +207,16 @@ export class Ajax<T = unknown> {
201207 }
202208 initOptions . headers = headers ;
203209
210+ if ( success ) {
211+ this . success ( success ) ;
212+ }
213+ if ( error ) {
214+ this . fail ( error ) ;
215+ }
216+ if ( complete ) {
217+ this . complete ( complete ) ;
218+ }
219+
204220 const beforeSends = [ ...( this . constructor as typeof Ajax ) . globalBeforeSends , beforeSend ] ;
205221 for ( const callback of beforeSends ) {
206222 if ( ! callback ) {
@@ -215,16 +231,6 @@ export class Ajax<T = unknown> {
215231 }
216232 }
217233
218- if ( success ) {
219- this . success ( success ) ;
220- }
221- if ( error ) {
222- this . fail ( error ) ;
223- }
224- if ( complete ) {
225- this . complete ( complete ) ;
226- }
227-
228234 if ( initOptions . signal ) {
229235 initOptions . signal . addEventListener ( 'abort' , ( ) => {
230236 this . abort ( ) ;
@@ -247,7 +253,15 @@ export class Ajax<T = unknown> {
247253 return [ ] ;
248254 }
249255 if ( this . _init ( ) === false ) {
250- return [ ] ;
256+ // beforeSend 返回 false,取消请求;标记为已完成并通知 error/complete。
257+ const abortError = this . _abortError || new Error ( 'abort' ) ;
258+ this . error = abortError ;
259+ this . _emit ( 'error' , abortError , undefined , abortError . message ) ;
260+ this . _emit ( 'complete' , undefined , undefined ) ;
261+ if ( this . setting . throws ) {
262+ throw abortError ;
263+ }
264+ return [ undefined , abortError , undefined ] ;
251265 }
252266
253267 const { timeout, dataType : dataTypeSetting , accepts, dataFilter, throws, jsonParser, convert} = this . setting ;
@@ -285,9 +299,11 @@ export class Ajax<T = unknown> {
285299 if ( convert ) {
286300 data = await convert ( data , dataType ) ;
287301 }
302+ if ( dataFilter ) {
303+ data = dataFilter ( data , dataType ) ?? data ;
304+ }
288305 this . data = data as T ;
289- const filteredData = dataFilter ?.( data , dataType ) ?? data ;
290- this . _emit ( 'success' , filteredData , statusText , response ) ;
306+ this . _emit ( 'success' , data , statusText , response ) ;
291307 } else {
292308 this . data = await response . text ( ) as T ;
293309 throw new Error ( statusText ) ;
0 commit comments