Skip to content

Commit a548f1f

Browse files
committed
ajax-browser: 遵守 AV._useMasterKey、在 Header 中发送 sessionToken
1 parent feccaee commit a548f1f

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

src/browserify-wrapper/ajax-browser.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ const ajax = (method, url, data, success, error) => {
3232
const appKey = AV.applicationKey;
3333
const masterKey = AV.masterKey;
3434

35-
// 清理原来多余的数据(如果不清理,会污染数据表)
36-
if (data) {
37-
delete data._ApplicationId;
38-
delete data._ApplicationKey;
39-
delete data._ApplicationProduction;
40-
delete data._MasterKey;
41-
delete data._ClientVersion;
42-
delete data._InstallationId;
43-
}
44-
4535
let handled = false;
4636
const xhr = new global.XMLHttpRequest();
4737
xhr.onreadystatechange = () => {
@@ -82,19 +72,39 @@ const ajax = (method, url, data, success, error) => {
8272
}
8373
}
8474

85-
xhr.open(method, url, true);
86-
xhr.setRequestHeader('X-LC-Id', appId);
75+
let headers = {
76+
'X-LC-Id': appId,
77+
'X-LC-UA': 'LC-Web-' + AV.version,
78+
'Content-Type': 'application/json;charset=UTF-8'
79+
};
80+
81+
// 清理原来多余的数据(如果不清理,会污染数据表)
82+
if (data) {
83+
delete data._ApplicationId;
84+
delete data._ApplicationKey;
85+
delete data._ApplicationProduction;
86+
delete data._MasterKey;
87+
delete data._ClientVersion;
88+
delete data._InstallationId;
8789

88-
let signature;
89-
if (masterKey) {
90-
signature = sign(masterKey, true);
90+
if (data._SessionToken) {
91+
headers['X-LC-Session'] = data._SessionToken;
92+
delete data._SessionToken;
93+
}
94+
}
95+
96+
if (masterKey && AV._useMasterKey) {
97+
headers['X-LC-Sign'] = sign(masterKey, true);
9198
} else {
92-
signature = sign(appKey);
99+
headers['X-LC-Sign'] = sign(appKey);
100+
}
101+
102+
xhr.open(method, url, true);
103+
104+
for (let name in headers) {
105+
xhr.setRequestHeader(name, headers[name]);
93106
}
94107

95-
xhr.setRequestHeader('X-LC-Sign', signature);
96-
xhr.setRequestHeader('X-LC-UA', 'LC-Web-' + AV.version);
97-
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
98108
xhr.send(JSON.stringify(data));
99109
return promise._thenRunCallbacks(options);
100110
};

0 commit comments

Comments
 (0)