Skip to content

Commit

Permalink
Added wx.request support.
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Nov 6, 2016
1 parent ba2394a commit 34aaaa7
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Ma Bingyao <[email protected]>",
"name": "hprose",
"version": "2.0.17",
"version": "2.0.18",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"keywords": [
"hprose",
Expand Down
9 changes: 6 additions & 3 deletions dist/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/hprose.min.js

Large diffs are not rendered by default.

35 changes: 32 additions & 3 deletions dist/hprose.src.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Hprose for JavaScript v2.0.17
// Hprose for JavaScript v2.0.18
// Copyright (c) 2008-2016 http://hprose.com
// Hprose is freely distributable under the MIT license.
// For all details and documentation:
Expand Down Expand Up @@ -6285,7 +6285,7 @@
* *
* hprose http client for JavaScript. *
* *
* LastModified: Sep 29, 2016 *
* LastModified: Nov 6, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -6323,6 +6323,8 @@
}
catch (e) {}

var wx = global.wx;

var localfile = (global.location !== undefined && global.location.protocol === 'file:');
var nativeXHR = (typeof(XMLHttpRequest) !== 'undefined');
var corsSupport = (!localfile && nativeXHR && 'withCredentials' in new XMLHttpRequest());
Expand Down Expand Up @@ -6517,6 +6519,31 @@
return future;
}

function wxPost(request, env) {
var future = new Future();
var header = {};
for (var k in _header) {
header[k] = _header[k];
}
header['Content-Type'] = 'text/plain; charset=UTF-8';
wx.request({
url: self.uri(),
method: 'POST',
data: request,
header: header,
timeout: env.timeout,
complete: function(ret) {
if (ret.statusCode === 200) {
future.resolve(ret.data);
}
else {
future.reject(new Error(ret.statusCode + ":" + ret.data));
}
}
});
return future;
}

function isCrossDomain() {
if (global.location === undefined) {
return true;
Expand All @@ -6537,7 +6564,9 @@
(env.binary || isCrossDomain()));
var apicloud = (typeof(global.api) !== "undefined" &&
typeof(global.api.ajax) !== "undefined");
var future = fhr ? fhrPost(request, env) :
var wxreq = wx && wx.request;
var future = wxreq ? wxPost(request, env) :
fhr ? fhrPost(request, env) :
apicloud ? apiPost(request, env) :
deviceone ? deviceOnePost(request, env) :
xhrPost(request, env);
Expand Down
9 changes: 6 additions & 3 deletions example/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hprose-js",
"filename": "hprose.js",
"version": "2.0.17",
"version": "2.0.18",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"homepage": "https://github.com/hprose",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/CopyRight.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Hprose for JavaScript v2.0.17
// Hprose for JavaScript v2.0.18
// Copyright (c) 2008-2016 http://hprose.com
// Hprose is freely distributable under the MIT license.
// For all details and documentation:
Expand Down
33 changes: 31 additions & 2 deletions src/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* *
* hprose http client for JavaScript. *
* *
* LastModified: Sep 29, 2016 *
* LastModified: Nov 6, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -50,6 +50,8 @@
}
catch (e) {}

var wx = global.wx;

var localfile = (global.location !== undefined && global.location.protocol === 'file:');
var nativeXHR = (typeof(XMLHttpRequest) !== 'undefined');
var corsSupport = (!localfile && nativeXHR && 'withCredentials' in new XMLHttpRequest());
Expand Down Expand Up @@ -244,6 +246,31 @@
return future;
}

function wxPost(request, env) {
var future = new Future();
var header = {};
for (var k in _header) {
header[k] = _header[k];
}
header['Content-Type'] = 'text/plain; charset=UTF-8';
wx.request({
url: self.uri(),
method: 'POST',
data: request,
header: header,
timeout: env.timeout,
complete: function(ret) {
if (ret.statusCode === 200) {
future.resolve(ret.data);
}
else {
future.reject(new Error(ret.statusCode + ":" + ret.data));
}
}
});
return future;
}

function isCrossDomain() {
if (global.location === undefined) {
return true;
Expand All @@ -264,7 +291,9 @@
(env.binary || isCrossDomain()));
var apicloud = (typeof(global.api) !== "undefined" &&
typeof(global.api.ajax) !== "undefined");
var future = fhr ? fhrPost(request, env) :
var wxreq = wx && wx.request;
var future = wxreq ? wxPost(request, env) :
fhr ? fhrPost(request, env) :
apicloud ? apiPost(request, env) :
deviceone ? deviceOnePost(request, env) :
xhrPost(request, env);
Expand Down
9 changes: 6 additions & 3 deletions test/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34aaaa7

Please sign in to comment.