Skip to content

Commit b4fa247

Browse files
committed
[fix] 浏览器中使用正确的 http method。
1 parent c65be54 commit b4fa247

File tree

6 files changed

+82
-34
lines changed

6 files changed

+82
-34
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<body>
88
<h1>LeanCloud JavaScript SDK</h1>
99
<p>打开控制台调试</p>
10-
<script src="../dist/av-es6.js"></script>
10+
<script src="../dist/av.js"></script>
1111
<script src="../test-es5.js"></script>
1212
</body>
1313
</html>

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 + '=' + 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', 'AV-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/utils.js

Lines changed: 4 additions & 22 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)) {
@@ -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
};

test/query.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,30 @@ describe('Queries', function () {
2525

2626
});
2727

28-
it();
29-
28+
it('should return TestClass', function(done) {
29+
var TestClass = AV.Object.extend('TestClass');
30+
var testObj = new TestClass();
31+
testObj.set({
32+
name: 'hjiang',
33+
phone: '123123123'
34+
});
35+
testObj.save().then(function() {
36+
var query = new AV.Query('TestClass');
37+
query.equalTo('name', 'hjiang');
38+
query.find().then(function(datas) {
39+
var obj = datas[0];
40+
if (obj && obj.get('name') === 'hjiang') {
41+
done();
42+
} else {
43+
done('failed');
44+
}
45+
}).catch(function(err) {
46+
done(err);
47+
});
48+
}).catch(function(err) {
49+
done(err);
50+
});
51+
});
3052

3153
it('should throw when get null', function () {
3254

@@ -334,7 +356,7 @@ describe('Queries', function () {
334356
});
335357

336358
describe('Compound Query', function () {
337-
it('should satisfy on 'or' conditions', function (done) {
359+
it('should satisfy on \'or\' conditions', function (done) {
338360
var lotsOfWins = new AV.Query('GameScore');
339361
lotsOfWins.greaterThan('score', 150);
340362

@@ -355,7 +377,7 @@ describe('Queries', function () {
355377
}
356378
});
357379
});
358-
it('should satisfy on 'and' conditions', function (done) {
380+
it('should satisfy on \'and\' conditions', function (done) {
359381
var lotsOfWins = new AV.Query('GameScore');
360382
lotsOfWins.greaterThan('score', 150);
361383

0 commit comments

Comments
 (0)