- fotas.net.call = function (uri, predefine, callback, param, timeout, retry)
- {
- var uniqid = fotas.uniqid();
- fotas.callbacks[uniqid] = function (a) {
- delete fotas.callbacks[uniqid];
- document.body.removeChild(fotas.scripts[uniqid]);
- delete fotas.scripts[uniqid];
- Ajax = new Object();
- Ajax.readyState = 4;
- Ajax.status = 200;
- Ajax.responseText = a;
- if (callback != undefined)
- callback(Ajax);
- };
- if (uri.indexOf('?') != -1)
- uri += "&jsonp="+encodeURIComponent("fotas.callbacks["+uniqid+"]")+"&key="+fotas.key;
- else
- uri += "?jsonp="+encodeURIComponent("fotas.callbacks["+uniqid+"]")+"&key="+fotas.key;
- if (param != undefined)
- uri += "&"+param;
- fotas.scripts[uniqid] = document.createElement('script');
- fotas.scripts[uniqid].setAttribute("type","text/javascript");
- fotas.scripts[uniqid].src = fotas.uri+uri;
- document.body.appendChild(fotas.scripts[uniqid]);
- }
- fotas.net.call = function (uri, predefine, callback, param, timeout, retry)
- {
- var Ajax = CreateXMLHttpRequest();
- Ajax.onreadystatechange = function () { callback(Ajax); };
- if (param != undefined)
- {
- Ajax.open("POST", uri, true);
- Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- Ajax.setRequestHeader("Connection", "close");
- if (predefine != undefined)
- Ajax = predefine(Ajax);
- Ajax.send(param);
- } else {
- Ajax.open("GET", uri, true);
- if (predefine != undefined)
- Ajax = predefine(Ajax);
- if (window.XMLHttpRequest)
- Ajax.send(null);
- else if (window.ActiveXObject)
- Ajax.send();
- }
- if (timeout != undefined)
- {
- var trynow = 0;
- if (retry == undefined)
- retry = 0;
- function ConnectionCheck()
- {
- if(Ajax.readyState != 4)
- {
- Ajax.close();
- trynow++;
- if (trynow <= retry)
- {
- if (param != undefined)
- {
- Ajax.open("POST", uri, true);
- Ajax.send(param);
- } else {
- Ajax.open("GET", uri, true);
- if (window.XMLHttpRequest)
- Ajax.send(null);
- else if (window.ActiveXObject)
- Ajax.send();
- }
- setTimeout(ConnectionCheck, timeout);
- }
- }
- }
-
- setTimeout(ConnectionCheck, timeout);
- }
- }
这两个fotas.net.call互相兼容。第一个用于跨域AJAX(JSONP),第二个是标准的XMLHttpRequest。因此第二个支持设置Timeout和Request Header。