33 * Each engineer has a duty to keep the code elegant
44**/
55
6- var Promise = require ( '../promise' ) ;
6+ 'use strict' ;
77
8- module . exports = function _ajax ( method , url , data , success , error ) {
9- var options = {
8+ const md5 = require ( 'md5' ) ;
9+ const AVPromise = require ( '../promise' ) ;
10+
11+ // 计算 X-LC-Sign 的签名方法
12+ const sign = ( key , isMasterKey ) => {
13+ const now = new Date ( ) . getTime ( ) ;
14+ const signature = md5 ( now + key ) ;
15+ if ( isMasterKey ) {
16+ return signature + ',' + now + ',master' ;
17+ } else {
18+ return signature + ',' + now ;
19+ }
20+ } ;
21+
22+ const ajax = ( method , url , data , success , error ) => {
23+ const AV = global . AV ;
24+
25+ const promise = new AVPromise ( ) ;
26+ const options = {
1027 success : success ,
1128 error : error
1229 } ;
1330
14- if ( useXDomainRequest ( ) ) {
15- return ajaxIE8 ( method , url , data ) . _thenRunCallbacks ( options ) ;
16- }
17-
18- var promise = new Promise ( ) ;
19- var handled = false ;
31+ const appId = AV . applicationId ;
32+ const appKey = AV . applicationKey ;
33+ const masterKey = AV . masterKey ;
2034
21- var xhr = new XMLHttpRequest ( ) ;
22- xhr . onreadystatechange = function ( ) {
35+ let handled = false ;
36+ const xhr = new global . XMLHttpRequest ( ) ;
37+ xhr . onreadystatechange = ( ) => {
2338 if ( xhr . readyState === 4 ) {
2439 if ( handled ) {
2540 return ;
2641 }
2742 handled = true ;
2843
2944 if ( xhr . status >= 200 && xhr . status < 300 ) {
30- var response ;
45+ let response ;
3146 try {
3247 response = JSON . parse ( xhr . responseText ) ;
3348 } catch ( e ) {
@@ -44,49 +59,13 @@ module.exports = function _ajax(method, url, data, success, error) {
4459 }
4560 } ;
4661 xhr . open ( method , url , true ) ;
47- xhr . setRequestHeader ( "Content-Type" , "text/plain" ) ; // avoid pre-flight.
62+ xhr . setRequestHeader ( 'X-LC-Id' , appId ) ;
63+ // 浏览器端不支持传入 masterKey 做 sign
64+ const signature = sign ( appKey ) ;
65+ xhr . setRequestHeader ( 'X-LC-Sign' , signature ) ;
66+ xhr . setRequestHeader ( 'Content-Type' , 'application/json' ) ;
4867 xhr . send ( data ) ;
4968 return promise . _thenRunCallbacks ( options ) ;
5069} ;
5170
52- function useXDomainRequest ( ) {
53- if ( typeof ( XDomainRequest ) !== "undefined" ) {
54- // We're in IE 8+.
55- if ( 'withCredentials' in new XMLHttpRequest ( ) ) {
56- // We're in IE 10+.
57- return false ;
58- }
59- return true ;
60- }
61- return false ;
62- }
63-
64- function ajaxIE8 ( method , url , data ) {
65- var promise = new Promise ( ) ;
66- var xdr = new XDomainRequest ( ) ;
67- xdr . onload = function ( ) {
68- var response ;
69- try {
70- response = JSON . parse ( xdr . responseText ) ;
71- } catch ( e ) {
72- promise . reject ( e ) ;
73- }
74- if ( response ) {
75- promise . resolve ( response ) ;
76- }
77- } ;
78- xdr . onerror = xdr . ontimeout = function ( ) {
79- // Let's fake a real error message.
80- var fakeResponse = {
81- responseText : JSON . stringify ( {
82- code : AV . Error . X_DOMAIN_REQUEST ,
83- error : "IE's XDomainRequest does not supply error info."
84- } )
85- } ;
86- promise . reject ( xdr ) ;
87- } ;
88- xdr . onprogress = function ( ) { } ;
89- xdr . open ( method , url ) ;
90- xdr . send ( data ) ;
91- return promise ;
92- }
71+ module . exports = ajax ;
0 commit comments