-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwebapi.js
52 lines (51 loc) · 1.63 KB
/
webapi.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var webApi = {
_server_host : 'https://promotion.devgo.top/github_remark',
_httpGet : function(url, callback){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if ( xhr.readyState === xhr.DONE ) {
if ( xhr.status === 200 || xhr.status === 0 ) {
if ( xhr.response ) {
callback( xhr.response );
} else {
console.warn( "[" + url + "] seems to be unreachable or file there is empty" );
}
} else {
console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
}
}
};
xhr.open( "GET", url, true );
xhr.responseType = "json";
xhr.send( null );
},
_jsonPost : function(url, data, callback){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if ( xhr.readyState === xhr.DONE ) {
if ( xhr.status === 200 || xhr.status === 0 ) {
callback( xhr.response );
} else {
console.error( "jsonPost err [" + url + "] [" + xhr.status + "]" );
}
}
};
xhr.open( "POST", url, true );
xhr.setRequestHeader("Content-Type", "application/json");
xhr.responseType = "json";
xhr.send( JSON.stringify(data) );
}
};
webApi.updateRemark = function(userToken, username, remark, callback){
var data = {'token':userToken, 'username':username, 'remark':remark};
this._jsonPost(this._server_host+'/updateRemark', data, function(result){
callback(result.success);
})
};
webApi.getRemark = function(userToken, username, callback){
var url = this._server_host+'/getRemark?token='+userToken+'&username='+username;
this._httpGet(url, function(result){
if(result.success)
callback(result.data);
})
};