cross.js
1.54 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
57
58
59
60
61
62
63
64
65
66
67
/**
* 跨域发送信息工具库
*/
var config = require('./config');
var yasPath = config.yasPath;
var yasImgDomain = config.yasImgDomain;
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;
};
//发送图片方式
exports.imgSend = function(param, callback) {
if (window.fromApp || window.qs.app_version || window.qs.appVersion) {
// 从app中打开的wap页场合,走app的上报逻辑
return;
}
var image = new Image(1, 1);
image.src = yasImgDomain + yasPath + '?' + param;
image.onload = function() {
image.onload = null;
if (callback) {
callback();
}
};
};
// app中打开wap页 数据上报
exports.appSend = function(data, callback) {
if (!window.fromApp) {
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', yasMobileDomain, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}