<script type="text/javascript">
document.domain = 'test.cn';
var $ = {};
$.xho = function () { //创建xmlhttprequest对象
var http_request = null;
if (window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MIME类别
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { //IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
return http_request;
};
$.rsc = function (obj, callfunc) {
return function () {
if (obj.readyState == 4) {
if (obj.status == 200) {
callfunc(obj.responseText);
}
}
};
};
$.GET = function (url, callfunc) {
var xho = $.xho();
if (xho == null) return;
xho.onreadystatechange = $.rsc(xho, callfunc); //设置回调函数
//第三个参数指定在等待服务器返回信息的时间内是否继续执行下面代码,如果为true,则不会继续执行,默认为true
xho.open("GET", url, true);
xho.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xho.send(null);
};
$.POST = function (url, data, callfunc) {
var xho = $.xho();
if (xho == null) return;
xho.onreadystatechange = $.rsc(xho, callfunc); //设置回调函数
xho.open("POST", url, true);
xho.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //post请求必须修改MIME类别
xho.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xho.send(data);
};
function call(func, param)
{
func = eval('parent.window.' + func);
func(param);
}
function isJson(obj){
var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
return isjson;
}
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
function json2Query(params) {
tail = [];
for (var p in params) {
if (params.hasOwnProperty(p)) {
tail.push(p + "=" + encodeURIComponent(params[p]));
}
}
return tail.join("&")
}
var invokeApi = function(api, param, method, callback) {
var m = (!method || method.toLowerCase() == 'get') ? 'get' : 'post';
if ('get' == m) {
debugger
if (isJson(param) && ! isEmpty(param)) {
param = json2Query(param);
}
if (param) {
api = (api.indexOf('?') == -1) ? api + '?' + param : api + '&' + param;
}
$.GET(
api,
function(data){
call(callback, data);
}
)
} else {
if (isJson(param)) {
param = json2Query(param);
}
$.POST(
api,
param,
function(data) {
call(callback, data);
}
)
}
}
</script>
说明:比如 a.test.cn的js想发送请求到 b.test.cn,这时只需要在 b.test.cn 的根目录新增一个 proxy.html ,内容为上面的代码。然后在 a.test.cn 下新建一个html文件,内容如下:
<script type="text/javascript">
mod = {};
mod.submod = function() {
var ifr_win = '';
var init = function() {
load_iframe();
}
var load_iframe = function() {
document.domain = 'test.cn';
var ifr = document.createElement('iframe');
ifr.src = 'http://b.test.cn/proxy.html';
ifr.style.display = 'none';
ifr.id = 'proxy_ifr';
document.body.appendChild(ifr);
ifr.onload = function() {
ifr_win = ifr.contentWindow;
};
};
var cb = function(msg) {
console.log(msg);
}
var get_dl = function() {
ifr_win.invokeApi(
'http://b.test.cn/ajax/dl',
{"id":13082, "from":"office", "app":"helper", "file_type":1},
'POST',
'mod.submod.cb'
);
}
var get_preview = function() {
ifr_win.invokeApi(
'http://b.test.cn/ajax/preview',
{"mb_id":12269, "moban_type":"2", "app":"helper", "file_type":2},
'GET',
'mod.submod.cb'
);
}
return {
init : init,
cb : cb,
get_dl : get_dl,
get_preview : get_preview
};
}();
mod.submod.init();
setTimeout("mod.submod.get_dl()", 500);
setTimeout("mod.submod.get_preview()", 500);
</script>
197

被折叠的 条评论
为什么被折叠?



