记录fotas.net.call的两个实现

  1. fotas.net.call = function (uri, predefine, callback, param, timeout, retry)
  2. {
  3.     var uniqid = fotas.uniqid();
  4.     fotas.callbacks[uniqid] = function (a) {
  5.         delete fotas.callbacks[uniqid];
  6.         document.body.removeChild(fotas.scripts[uniqid]);
  7.         delete fotas.scripts[uniqid];
  8.         Ajax = new Object();
  9.         Ajax.readyState = 4;
  10.         Ajax.status = 200;
  11.         Ajax.responseText = a;
  12.         if (callback != undefined)
  13.             callback(Ajax);
  14.     };
  15.     if (uri.indexOf('?') != -1)
  16.         uri += "&jsonp="+encodeURIComponent("fotas.callbacks["+uniqid+"]")+"&key="+fotas.key;
  17.     else
  18.         uri += "?jsonp="+encodeURIComponent("fotas.callbacks["+uniqid+"]")+"&key="+fotas.key;
  19.     if (param != undefined)
  20.         uri += "&"+param;
  21.     fotas.scripts[uniqid] = document.createElement('script');
  22.     fotas.scripts[uniqid].setAttribute("type","text/javascript");
  23.     fotas.scripts[uniqid].src = fotas.uri+uri;
  24.     document.body.appendChild(fotas.scripts[uniqid]);
  25. }

  1. fotas.net.call = function (uri, predefine, callback, param, timeout, retry)
  2. {
  3.     var Ajax = CreateXMLHttpRequest();
  4.     Ajax.onreadystatechange = function () { callback(Ajax); };
  5.     if (param != undefined)
  6.     {
  7.         Ajax.open("POST", uri, true);
  8.         Ajax.setRequestHeader("Content-type""application/x-www-form-urlencoded");
  9.         Ajax.setRequestHeader("Connection""close");
  10.         if (predefine != undefined)
  11.             Ajax = predefine(Ajax);
  12.         Ajax.send(param);
  13.     } else {
  14.         Ajax.open("GET", uri, true);
  15.         if (predefine != undefined)
  16.             Ajax = predefine(Ajax);
  17.         if (window.XMLHttpRequest)
  18.             Ajax.send(null);
  19.         else if (window.ActiveXObject)
  20.             Ajax.send();
  21.     }
  22.     if (timeout != undefined)
  23.     {
  24.         var trynow = 0;
  25.         if (retry == undefined)
  26.             retry = 0;
  27.         function ConnectionCheck()
  28.         {  
  29.             if(Ajax.readyState != 4)
  30.             {
  31.                 Ajax.close();
  32.                 trynow++;
  33.                 if (trynow <= retry)
  34.                 {
  35.                     if (param != undefined)
  36.                     {
  37.                         Ajax.open("POST", uri, true);
  38.                         Ajax.send(param);
  39.                     } else {
  40.                         Ajax.open("GET", uri, true);
  41.                         if (window.XMLHttpRequest)
  42.                             Ajax.send(null);
  43.                         else if (window.ActiveXObject)
  44.                             Ajax.send();
  45.                     }
  46.                     setTimeout(ConnectionCheck, timeout);
  47.                 }
  48.             }
  49.         }
  50.         
  51.         setTimeout(ConnectionCheck, timeout);
  52.     }
  53. }
这两个fotas.net.call互相兼容。第一个用于跨域AJAX(JSONP),第二个是标准的XMLHttpRequest。因此第二个支持设置Timeout和Request Header。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值