var SpacesApi host http spaces ru get function method args callback on

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var SpacesApi = {
host: "http://spaces.ru",
get: function (method, args, callback, on_error) {
return SpacesApi.do(method, "GET", args, callback);
},
post: function (method, args, callback, on_error) {
return SpacesApi.do(method, "POST", args, callback);
},
do: function (method, type, args, callback) {
var p = method.split(".");
if (!args._origin)
args._origin = location.protocol + "//" + location.host;
return $.ajax({
type: type,
url: SpacesApi.host + "/api/" + p[0] + "/?method=" + encodeURIComponent(p[1]),
data: SpacesApi.serialize(args),
dataType: "json"
}).done(callback);
},
serialize: function (args) {
var url_args = [];
for (var k in args) {
if (args[k] === undefined)
continue;
if (args[k] instanceof Array) {
for (var i in args[k])
url_args.push(k + "=" + encodeURIComponent(args[k][i]));
} else if (args[k] instanceof Boolean) {
url_args.push(k + "=" + (args[k] ? 1 : 0));
} else
url_args.push(k + "=" + encodeURIComponent(args[k]));
}
return url_args.join("&");
}
};