cross.js 1.15 KB
/**
 * 跨域发送信息工具库
 */
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);
};