66'use strict' ;
77
88const _ = require ( 'underscore' ) ;
9- const ajax = require ( './browserify-wrapper/ ajax' ) ;
9+ const ajax = require ( './ajax' ) ;
1010const Cache = require ( './cache' ) ;
1111const md5 = require ( 'md5' ) ;
1212const debug = require ( 'debug' ) ( 'utils' ) ;
@@ -394,6 +394,8 @@ const init = (AV) => {
394394 throw "Bad route: '" + route + "'." ;
395395 }
396396
397+ dataObject = dataObject || { } ;
398+
397399 // 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
398400 let apiURL = AV . serverURL || AVConfig . APIServerURL ;
399401 if ( AV . serverURL ) {
@@ -422,21 +424,8 @@ const init = (AV) => {
422424 }
423425 }
424426
425- if ( method . toLowerCase ( ) === 'get' ) {
426- if ( apiURL . indexOf ( '?' ) === - 1 ) {
427- apiURL += '?' ;
428- }
429- for ( let k in dataObject ) {
430- if ( typeof dataObject [ k ] === 'object' ) {
431- dataObject [ k ] = JSON . stringify ( dataObject [ k ] ) ;
432- }
433- apiURL += '&' + k + '=' + encodeURIComponent ( dataObject [ k ] ) ;
434- }
435- }
436-
437427 var headers = {
438428 'X-LC-Id' : AV . applicationId ,
439- 'X-LC-UA' : 'LC-Web-' + AV . version ,
440429 'Content-Type' : 'application/json;charset=UTF-8'
441430 } ;
442431 if ( AV . masterKey && AV . _useMasterKey ) {
@@ -447,6 +436,8 @@ const init = (AV) => {
447436 if ( ! AV . _isNullOrUndefined ( AV . applicationProduction ) ) {
448437 headers [ 'X-LC-Prod' ] = AV . applicationProduction ;
449438 }
439+ headers [ 'User-Agent' ] = AV . _config . userAgent || `AV/${ AV . version } ; Node.js/${ process . version } ` ;
440+
450441 return AV . Promise . as ( ) . then ( function ( ) {
451442 // Pass the session token
452443 if ( sessionToken ) {
@@ -459,18 +450,60 @@ const init = (AV) => {
459450 } ) ;
460451 }
461452 } ) . then ( function ( ) {
462- return AV . _ajax ( method , apiURL , JSON . stringify ( dataObject ) , headers ) . then ( null , function ( response ) {
453+ // redirection for request with pre-flight is not supported by browser
454+ // fallback to plain POST
455+ if ( ! AVConfig . isNode ) {
456+ try {
457+ if ( _ . isObject ( dataObject ) ) {
458+ if ( method . toLowerCase ( ) !== 'post' ) {
459+ dataObject . _method = method ;
460+ method = 'post' ;
461+ }
462+ dataObject . _ApplicationId = headers [ 'X-LC-Id' ] ;
463+ // TODO: use X-LC-Sign
464+ dataObject . _ApplicationKey = AV . applicationKey ;
465+ if ( AV . _useMasterKey ) {
466+ dataObject . _MasterKey = AV . masterKey ;
467+ }
468+ dataObject . _ApplicationProduction = headers [ 'X-LC-Prod' ] ;
469+ dataObject . _ClientVersion = 'LC-Web-' + AV . version ;
470+ dataObject . _SessionToken = headers [ 'X-LC-Session' ] ;
471+ headers = {
472+ 'content-type' : 'text/plain' ,
473+ } ;
474+ dataObject = JSON . stringify ( dataObject ) ;
475+ }
476+ } catch ( e ) { }
477+ }
478+
479+ if ( method . toLowerCase ( ) === 'get' ) {
480+ if ( apiURL . indexOf ( '?' ) === - 1 ) {
481+ apiURL += '?' ;
482+ }
483+ for ( let k in dataObject ) {
484+ if ( typeof dataObject [ k ] === 'object' ) {
485+ dataObject [ k ] = JSON . stringify ( dataObject [ k ] ) ;
486+ }
487+ apiURL += '&' + k + '=' + encodeURIComponent ( dataObject [ k ] ) ;
488+ }
489+ }
490+
491+ return AV . _ajax ( method , apiURL , dataObject , headers ) . then ( null , function ( response ) {
463492 // Transform the error into an instance of AV.Error by trying to parse
464493 // the error string as JSON.
465494 var error ;
466- if ( response && response . responseText ) {
467- try {
468- var errorJSON = JSON . parse ( response . responseText ) ;
469- if ( errorJSON ) {
470- error = new AV . Error ( errorJSON . code , errorJSON . error ) ;
495+ if ( response ) {
496+ if ( response . response ) {
497+ error = new AV . Error ( response . response . code , response . response . error ) ;
498+ } else if ( response . responseText ) {
499+ try {
500+ var errorJSON = JSON . parse ( response . responseText ) ;
501+ if ( errorJSON ) {
502+ error = new AV . Error ( errorJSON . code , errorJSON . error ) ;
503+ }
504+ } catch ( e ) {
505+ // If we fail to parse the error text, that's okay.
471506 }
472- } catch ( e ) {
473- // If we fail to parse the error text, that's okay.
474507 }
475508 }
476509 error = error || new AV . Error ( - 1 , response . responseText ) ;
0 commit comments