cross.js
1.15 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
/**
* 跨域发送信息工具库
*/
var config = require('./config');
var yasMobileDomain = config.yasMobileDomain;
var createCORSRequest = function(method, url) {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (typeof XDomainRequest != 'undefined') {
// IE8 & IE9
xhr = new XDomainRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
return xhr;
};
// app中打开wap页 数据上报
exports.appSend = function(data, callback) {
if (!window.appBaseLogs) {
return;
}
var xhr = createCORSRequest();
if (!xhr) {
return;
}
xhr.ontimeout = function(e) {
console.log('timeout: ', JSON.stringify(e));
};
xhr.onerror = function(e) {
console.log('error: ', JSON.stringify(e));
};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200 && callback) {
callback();
}
}
};
xhr.open('post', (document.location.protocol === 'https:' ? 'https:' : 'http:') + yasMobileDomain, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(data);
};