Skip to content

Commit 34f42a7

Browse files
committed
Merge pull request #237 from wangxiao/bug
修正浏览器中的请求方式
2 parents c5e2c5a + 380806c commit 34f42a7

File tree

12 files changed

+189
-110
lines changed

12 files changed

+189
-110
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ JavaScript SDK for [LeanCloud](http://leancloud.cn/).
5454
* 打包(执行 `gulp release`
5555
* 提交当前所有代码
5656
* 版本号相关修改
57-
* change log
57+
* 对照 commit 历史写 change log
5858
* dist/ 目录中的新代码
5959
* 提交代码,发 Pull Request
6060
* 通过 review,merge 代码
6161
* Github 生成 release 包(for bower)
6262
* 发布到 npm,需 npm 协作者身份(执行 `npm publish`
6363
* 发布到 CDN,需要七牛权限(执行 `gulp upload`
64+
* 提醒所有相关工程师完善文档
65+

demo/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
<link rel="stylesheet" href="style.css">
66
</head>
77
<body>
8-
<h1>LeanCloud JavaScript SDK</h1>
9-
<p>打开控制台调试</p>
10-
<script src="../dist/av-es6.js"></script>
8+
<h1><a href="https://leancloud.cn">LeanCloud</a></h1>
9+
<h2><a href="https://leancloud.cn">为开发加速</a></h2>
10+
<div class="tips">
11+
<p>欢迎调试 JavaScript SDK,请打开浏览器控制台</p>
12+
</div>
13+
<script src="../dist/av.js"></script>
1114
<script src="../test-es5.js"></script>
1215
</body>
1316
</html>

demo/style.css

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
15
body {
6+
background: #fff;
27
text-align: center;
8+
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
9+
}
10+
a {
11+
text-decoration: none;
312
}
4-
513
h1 {
6-
margin-top: 30%;
7-
font-size: 20px;
14+
margin-top: 120px;
15+
font-size: 60px;
16+
font-weight: bold;
17+
}
18+
h1 a {
19+
color: #2C97E8;
20+
}
21+
h2 {
22+
letter-spacing: 10px;
23+
font-size: 26px;
24+
}
25+
h2 a {
26+
color: #000;
27+
}
28+
.tips {
29+
margin-top: 90px;
30+
font-size: 15px;
31+
font-weight: bold;
32+
letter-spacing: 1px;
33+
line-height: 25px;
34+
color: #E82C4B;
835
}
936

demo/test-es5.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ AV.init({
88
appKey: appKey
99
});
1010

11+
// 基本存储
1112
var TestClass = AV.Object.extend('TestClass');
1213
var testObj = new TestClass();
1314
testObj.set({
1415
name: 'hjiang',
1516
phone: '123123123'
1617
});
18+
1719
testObj.save().then(function () {
1820
console.log('success');
1921
}).catch(function (err) {
2022
console.log('failed');
2123
console.log(err);
2224
});
2325

26+
// 存储文件
2427
var base64 = 'd29ya2luZyBhdCBhdm9zY2xvdWQgaXMgZ3JlYXQh';
2528
var file = new AV.File('myfile.txt', { base64: base64 });
2629
file.metaData('format', 'txt file');

demo/test-es6.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ AV.init({
66
appKey: appKey
77
});
88

9+
// 基本存储
910
const TestClass = AV.Object.extend('TestClass');
1011
const testObj = new TestClass();
1112
testObj.set({
1213
name: 'hjiang',
1314
phone: '123123123'
1415
});
16+
1517
testObj.save().then(() => {
1618
console.log('success');
1719
}).catch((err) => {
1820
console.log('failed');
1921
console.log(err);
2022
});
2123

24+
// 存储文件
2225
const base64 = 'd29ya2luZyBhdCBhdm9zY2xvdWQgaXMgZ3JlYXQh';
2326
var file = new AV.File('myfile.txt', { base64: base64 });
2427
file.metaData('format', 'txt file');

gulpfile.babel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ gulp.task('test', [
199199

200200
// 上传到 CDN
201201
gulp.task('upload', ['compress-scripts'], (cb) => {
202-
uploadCDN('./dist/av-mini.js', getAVVersion(), () => {});
202+
uploadCDN('./dist/av-min.js', getAVVersion(), () => {});
203203
});
204204

205205
// 生成 release 文件

src/browserify-wrapper/ajax-browser.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
'use strict';
77

88
const AVPromise = require('../promise');
9-
const AVUtils = require('../utils');
9+
const md5 = require('md5');
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+
};
1021

1122
const ajax = (method, url, data, success, error) => {
1223
const AV = global.AV;
@@ -21,6 +32,16 @@ const ajax = (method, url, data, success, error) => {
2132
const appKey = AV.applicationKey;
2233
const masterKey = AV.masterKey;
2334

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+
2445
let handled = false;
2546
const xhr = new global.XMLHttpRequest();
2647
xhr.onreadystatechange = () => {
@@ -47,19 +68,34 @@ const ajax = (method, url, data, success, error) => {
4768
}
4869
}
4970
};
71+
72+
if (method.toLowerCase() === 'get') {
73+
let i = 0;
74+
for (let k in data) {
75+
if (i === 0) {
76+
url = url + '?';
77+
} else {
78+
url = url + '&';
79+
}
80+
url = url + k + '=' + encodeURIComponent(JSON.stringify(data[k]));
81+
i ++;
82+
}
83+
}
84+
5085
xhr.open(method, url, true);
5186
xhr.setRequestHeader('X-LC-Id', appId);
5287

5388
let signature;
5489
if (masterKey) {
55-
signature = AVUtils.sign(masterKey, true);
90+
signature = sign(masterKey, true);
5691
} else {
57-
signature = AVUtils.sign(appKey);
92+
signature = sign(appKey);
5893
}
5994

6095
xhr.setRequestHeader('X-LC-Sign', signature);
96+
xhr.setRequestHeader('X-LC-UA', 'LC-Web-' + AV.version);
6197
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
62-
xhr.send(data);
98+
xhr.send(JSON.stringify(data));
6399
return promise._thenRunCallbacks(options);
64100
};
65101

src/browserify-wrapper/ajax.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ var https = require('https');
1010
var url = require('url');
1111

1212
var Promise = require('../promise');
13-
var AV_VERSION = require('../version')
1413

1514
// `keepAlive` option only work on Node.js 0.12+
1615
var httpAgent = new http.Agent({keepAlive: true});
1716
var httpsAgent = new https.Agent({keepAlive: true});
1817

1918
module.exports = function _ajax(method, resourceUrl, data, success, error) {
19+
if (method.toLowerCase() !== 'post') {
20+
data = data || {};
21+
data._method = method;
22+
method = 'post';
23+
}
24+
data = JSON.stringify(data);
25+
2026
var parsedUrl = url.parse(resourceUrl);
2127
var promise = new Promise();
2228

@@ -37,7 +43,7 @@ module.exports = function _ajax(method, resourceUrl, data, success, error) {
3743
agent: transportAgent,
3844
headers: {
3945
'Content-Type': 'text/plain',
40-
'User-Agent': 'AV/' + AV_VERSION + ' (Node.js' + process.version + ')'
46+
'User-Agent': 'AV/' + AV.version + ' (Node.js' + process.version + ')'
4147
}
4248
});
4349

src/browserify-wrapper/upload-browser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = function upload(file, AV, saveOptions) {
2020
var uptoken = response.token;
2121

2222
var data = new FormData();
23-
data.append("file", dataFormat, self._name);
23+
data.append("file", dataFormat);
24+
data.append('name', self._name);
2425
data.append("key", self._qiniu_key);
2526
data.append("token", uptoken);
2627

src/utils.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
const _ = require('underscore');
9-
const md5 = require('md5');
9+
const ajax = require('./browserify-wrapper/ajax');
1010

1111
const init = (AV) => {
1212

@@ -118,7 +118,6 @@ const init = (AV) => {
118118
case 'us':
119119
AVConfig.region = 'us';
120120
break;
121-
case 'cn':
122121
default:
123122
AVConfig.region = 'cn';
124123
break;
@@ -300,7 +299,7 @@ const init = (AV) => {
300299
return new Date(Date.UTC(year, month, day, hour, minute, second, milli));
301300
};
302301

303-
AV._ajax = require('./browserify-wrapper/ajax');
302+
AV._ajax = ajax;
304303

305304
// A self-propagating extend function.
306305
AV._extend = function(protoProps, classProps) {
@@ -381,11 +380,6 @@ const init = (AV) => {
381380
}
382381

383382
dataObject = _.clone(dataObject || {});
384-
if (method !== "POST") {
385-
dataObject._method = method;
386-
method = "POST";
387-
}
388-
389383
dataObject._ApplicationId = AV.applicationId;
390384
dataObject._ApplicationKey = AV.applicationKey;
391385
if (!AV._isNullOrUndefined(AV.applicationProduction)) {
@@ -394,7 +388,7 @@ const init = (AV) => {
394388
if (AV._useMasterKey) {
395389
dataObject._MasterKey = AV.masterKey;
396390
}
397-
dataObject._ClientVersion = AV.VERSION;
391+
dataObject._ClientVersion = AV.version;
398392
// Pass the session token on every request.
399393
return AV.User.currentAsync().then(function(currentUser) {
400394
if (currentUser && currentUser._sessionToken) {
@@ -404,8 +398,7 @@ const init = (AV) => {
404398
}).then(function(_InstallationId) {
405399
dataObject._InstallationId = _InstallationId;
406400

407-
var data = JSON.stringify(dataObject);
408-
return AV._ajax(method, apiURL, data).then(null, function(response) {
401+
return AV._ajax(method, apiURL, dataObject).then(null, function(response) {
409402
// Transform the error into an instance of AV.Error by trying to parse
410403
// the error string as JSON.
411404
var error;
@@ -677,16 +670,5 @@ const init = (AV) => {
677670

678671
module.exports = {
679672

680-
init: init,
681-
682-
// 计算 X-LC-Sign 的签名方法
683-
sign: (key, isMasterKey) => {
684-
const now = new Date().getTime();
685-
const signature = md5(now + key);
686-
if (isMasterKey) {
687-
return signature + ',' + now + ',master';
688-
} else {
689-
return signature + ',' + now;
690-
}
691-
}
673+
init: init
692674
};

0 commit comments

Comments
 (0)