forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.js
More file actions
56 lines (51 loc) · 1.84 KB
/
Http.js
File metadata and controls
56 lines (51 loc) · 1.84 KB
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
53
54
55
56
/* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
/**
* @private
* @class SuperMap.LevelRenderer.Tool.Http
* @category Visualization Theme
* @classdesc LevelRenderer 工具-Http
*/
export class Http {
/**
* @function SuperMap.LevelRenderer.Tool.Http.constructor
* @description 构造函数。
*/
constructor() {
this.CLASS_NAME = "SuperMap.LevelRenderer.Tool.Http"
}
/**
* @function SuperMap.LevelRenderer.Tool.Http.prototype.get
* @description get请求。
* @param {(string|IHTTPGetOption)} url - 请求url
* @param {function} onsuccess - 请求成功函数
* @param {function} onerror - 请求失败函数
* @param {Object} opts - 额外参数
* @returns {number} cos值
*/
get(url, onsuccess, onerror) {
if (typeof(url) === 'object') {
var obj = url;
url = obj.url;
onsuccess = obj.onsuccess;
onerror = obj.onerror;
}
var xhr = window.XMLHttpRequest
? new XMLHttpRequest()
: new window.ActiveXObject('Microsoft.XMLHTTP');
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
onsuccess && onsuccess(xhr.responseText);
} else {
onerror && onerror();
}
xhr.onreadystatechange = new Function();
xhr = null;
}
};
xhr.send(null);
}
}