1 | var jquery = require("jquery");
|
2 |
|
3 | function send(method, url, body) {
|
4 | return new Promise(function (fulfil, reject) {
|
5 | jquery.ajax({
|
6 | url: url,
|
7 | type: method,
|
8 | contentType: "application/json; charset=UTF-8",
|
9 | data: JSON.stringify(body),
|
10 | success: function (data, textStatus, jqXHR) {
|
11 | fulfil({
|
12 | statusCode: jqXHR.status,
|
13 | body: data
|
14 | });
|
15 | },
|
16 | error: function (jqXHR, textStatus, error) {
|
17 | reject({
|
18 | statusCode: jqXHR.status,
|
19 | body: jqXHR.responseJSON
|
20 | });
|
21 | }
|
22 | });
|
23 | });
|
24 | }
|
25 |
|
26 | ['get', 'delete'].forEach(function (method) {
|
27 | module.exports[method] = function (url) {
|
28 | return send(method.toUpperCase(), url);
|
29 | };
|
30 | });
|
31 |
|
32 | ['put', 'post'].forEach(function (method) {
|
33 | module.exports[method] = function (url, body) {
|
34 | return send(method.toUpperCase(), url, body);
|
35 | };
|
36 | });
|