cross.js 1.19 KB
/**
* 跨域发送信息工具库
*/
var config = require('./config');

var yasPath = config.yasPath;

var yasImgDomain = config.yasImgDomain;

//发送图片方式
exports.imgSend = function (param, callback)
{
    var image = new Image(1, 1);
    image.src = yasImgDomain+yasPath+'ya.gif?'+param;
    image.onload = function()
    {
        image.onload = null;
        if(callback)
        {
          callback();
        }
    };
};

// ajax方式
// 需要服务端设置 Access-Control-Allow-Origin *,Access-Control-Allow-Credentials = true,
exports.ajaxSend = function (param, callback)
{
	var request,Request = window.XDomainRequest;
	if(Request)
	{
		request = new Request();
		request.open("POST", yasPath);
	}
	else
	{
		if(window.XMLHttpRequest)
		{
			request = new window.XMLHttpRequest();
		}
		else
		{
			Request = new window.ActiveXObject("Microsoft.XMLHTTP");
		}
		request.open("POST", yasPath, _true);
		request.setRequestHeader("Content-Type", "text/plain");
	}

	if(request)
	{
		request.onreadystatechange = function()
		{
			if(request.readyState == 4)
			{
				if(callback)
				{
					callback();
				}
				request = null;
			}
		};
		request.send(param);
		return true;
	}
	return false;
};