|
1 | 1 |
|
2 | | -var getUsers = function(onSuccess, onError) |
3 | | -{ |
| 2 | +var getUsers = function(onSuccess, onError) { |
4 | 3 | var xhr = new XMLHttpRequest(); |
5 | 4 | xhr.open('GET', '/users', true); |
6 | | - xhr.setRequestHeader("Accept","application/json"); |
7 | | - xhr.onreadystatechange = function (e) { |
8 | | - if (xhr.readyState == 4) { |
9 | | - if (xhr.status == 204 || xhr.status == 205) { |
| 5 | + xhr.setRequestHeader('Accept', 'application/json'); |
| 6 | + xhr.onreadystatechange = function () { |
| 7 | + var res = null; |
| 8 | + if (xhr.readyState === 4) { |
| 9 | + if (xhr.status === 204 || xhr.status === 205) { |
10 | 10 | onSuccess(); |
11 | 11 | } else if (xhr.status >= 200 && xhr.status < 300) { |
12 | | - var value = JSON.parse(xhr.responseText); |
13 | | - onSuccess(value); |
| 12 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 13 | + if (res) onSuccess(res); |
14 | 14 | } else { |
15 | | - var value = JSON.parse(xhr.responseText); |
16 | | - onError(value); |
| 15 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 16 | + if (res) onError(res); |
17 | 17 | } |
18 | 18 | } |
19 | | - } |
| 19 | + }; |
20 | 20 | xhr.send(null); |
21 | | -} |
| 21 | +}; |
22 | 22 |
|
23 | | -var getUsersByName = function(name, onSuccess, onError) |
24 | | -{ |
| 23 | +var getUsersByName = function(name, onSuccess, onError) { |
25 | 24 | var xhr = new XMLHttpRequest(); |
26 | 25 | xhr.open('GET', '/users/' + encodeURIComponent(name) + '', true); |
27 | | - xhr.setRequestHeader("Accept","application/json"); |
28 | | - xhr.onreadystatechange = function (e) { |
29 | | - if (xhr.readyState == 4) { |
30 | | - if (xhr.status == 204 || xhr.status == 205) { |
| 26 | + xhr.setRequestHeader('Accept', 'application/json'); |
| 27 | + xhr.onreadystatechange = function () { |
| 28 | + var res = null; |
| 29 | + if (xhr.readyState === 4) { |
| 30 | + if (xhr.status === 204 || xhr.status === 205) { |
31 | 31 | onSuccess(); |
32 | 32 | } else if (xhr.status >= 200 && xhr.status < 300) { |
33 | | - var value = JSON.parse(xhr.responseText); |
34 | | - onSuccess(value); |
| 33 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 34 | + if (res) onSuccess(res); |
35 | 35 | } else { |
36 | | - var value = JSON.parse(xhr.responseText); |
37 | | - onError(value); |
| 36 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 37 | + if (res) onError(res); |
38 | 38 | } |
39 | 39 | } |
40 | | - } |
| 40 | + }; |
41 | 41 | xhr.send(null); |
42 | | -} |
| 42 | +}; |
43 | 43 |
|
44 | | -var postUsers = function(body, onSuccess, onError) |
45 | | -{ |
| 44 | +var postUsers = function(body, onSuccess, onError) { |
46 | 45 | var xhr = new XMLHttpRequest(); |
47 | 46 | xhr.open('POST', '/users', true); |
48 | | - xhr.setRequestHeader("Accept","application/json"); |
49 | | - xhr.setRequestHeader("Content-Type","application/json"); |
50 | | - xhr.onreadystatechange = function (e) { |
51 | | - if (xhr.readyState == 4) { |
52 | | - if (xhr.status == 204 || xhr.status == 205) { |
| 47 | + xhr.setRequestHeader('Accept', 'application/json'); |
| 48 | + xhr.setRequestHeader('Content-Type', 'application/json'); |
| 49 | + xhr.onreadystatechange = function () { |
| 50 | + var res = null; |
| 51 | + if (xhr.readyState === 4) { |
| 52 | + if (xhr.status === 204 || xhr.status === 205) { |
53 | 53 | onSuccess(); |
54 | 54 | } else if (xhr.status >= 200 && xhr.status < 300) { |
55 | | - var value = JSON.parse(xhr.responseText); |
56 | | - onSuccess(value); |
| 55 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 56 | + if (res) onSuccess(res); |
57 | 57 | } else { |
58 | | - var value = JSON.parse(xhr.responseText); |
59 | | - onError(value); |
| 58 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 59 | + if (res) onError(res); |
60 | 60 | } |
61 | 61 | } |
62 | | - } |
63 | | - xhr.send(JSON.stringify(body) |
64 | | -); |
65 | | -} |
| 62 | + }; |
| 63 | + xhr.send(JSON.stringify(body)); |
| 64 | +}; |
66 | 65 |
|
67 | | -var getMetrics = function(onSuccess, onError) |
68 | | -{ |
| 66 | +var getMetrics = function(onSuccess, onError) { |
69 | 67 | var xhr = new XMLHttpRequest(); |
70 | 68 | xhr.open('GET', '/metrics', true); |
71 | | - xhr.setRequestHeader("Accept","application/json"); |
72 | | - xhr.onreadystatechange = function (e) { |
73 | | - if (xhr.readyState == 4) { |
74 | | - if (xhr.status == 204 || xhr.status == 205) { |
| 69 | + xhr.setRequestHeader('Accept', 'application/json'); |
| 70 | + xhr.onreadystatechange = function () { |
| 71 | + var res = null; |
| 72 | + if (xhr.readyState === 4) { |
| 73 | + if (xhr.status === 204 || xhr.status === 205) { |
75 | 74 | onSuccess(); |
76 | 75 | } else if (xhr.status >= 200 && xhr.status < 300) { |
77 | | - var value = JSON.parse(xhr.responseText); |
78 | | - onSuccess(value); |
| 76 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 77 | + if (res) onSuccess(res); |
79 | 78 | } else { |
80 | | - var value = JSON.parse(xhr.responseText); |
81 | | - onError(value); |
| 79 | + try { res = JSON.parse(xhr.responseText); } catch (e) { onError(e); } |
| 80 | + if (res) onError(res); |
82 | 81 | } |
83 | 82 | } |
84 | | - } |
| 83 | + }; |
85 | 84 | xhr.send(null); |
86 | | -} |
| 85 | +}; |
0 commit comments