記錄fotas.net.call的兩個實現

來源:互聯網
上載者:User
  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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.