Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TYSCRIPT = TYSCRIPT.extend({
  2.     get: function(url, data, callback, type) {
  3.         // shift arguments if data argument was omited
  4.         if (TYSCRIPT.isFunction(data))
  5.         {
  6.             type = type || callback;
  7.             callback = data;
  8.             data = null;
  9.         }
  10.         return TYSCRIPT.ajax({
  11.             type: "GET",
  12.             url: url,
  13.             data: data,
  14.             success: callback,
  15.             dataType: type
  16.         });
  17.     },
  18.  
  19.     getScript: function(url, callback) {
  20.         return TYSCRIPT.get(url, null, callback, "script");
  21.     },
  22.  
  23.     getJSON: function(url, data, callback) {
  24.         return TYSCRIPT.get(url, data, callback, "json");
  25.     },
  26.  
  27.     getXML: function(url, data, callback) {
  28.         return TYSCRIPT.get(url, data, callback, "xml");
  29.     },
  30.    
  31.     post: function(url, data, callback, type) {
  32.         // shift arguments if data argument was omited
  33.         if (TYSCRIPT.isFunction(data))
  34.         {
  35.             type = type || callback;
  36.             callback = data;
  37.             data = {};
  38.         }
  39.  
  40.         return TYSCRIPT.ajax({
  41.             type: "POST",
  42.             url: url,
  43.             data: data,
  44.             success: callback,
  45.             dataType: type
  46.         });
  47.     },
  48.  
  49.     ajaxSetup: function(settings)
  50.     {
  51.         TYSCRIPT.extend(TYSCRIPT.ajaxSettings, settings);
  52.     },
  53.  
  54.     ajaxSettings: {
  55.         url: '',
  56.         global: true,
  57.         type: "GET",
  58.         contentType: "application/x-www-form-urlencoded",
  59.         processData: true,
  60.         async: true,
  61.         /*
  62.         timeout: 0,
  63.         data: null,
  64.         username: null,
  65.         password: null,
  66.         traditional: false,
  67.         */
  68.         // This function can be overriden by calling TYSCRIPT.ajaxSetup
  69.         xhr: function()
  70.         {
  71.             try
  72.             {
  73.                 return new ActiveXObject("Microsoft.XMLHTTP");
  74.             }
  75.             catch(e)
  76.             {
  77.                 return new ActiveXObject("Msxml2.XMLHTTP");
  78.             }
  79.         },
  80.         accepts: {
  81.             xml: "application/xml, text/xml",
  82.             html: "text/html",
  83.             script: "text/javascript, application/javascript",
  84.             json: "application/json, text/javascript",
  85.             text: "text/plain",
  86.             _default: "*/*"
  87.         }
  88.     },
  89.  
  90.     ajax: function(origSettings)
  91.     {
  92.         var s = TYSCRIPT.extend(true, {}, TYSCRIPT.ajaxSettings, origSettings),
  93.             status, data, type = s.type.toUpperCase();
  94.            
  95.         Interop.Call("wininet.dll", "DeleteUrlCacheEntryW", s.url); //clear cache
  96.        
  97.         s.url = s.url.replace(/#.*$/, "");
  98.        
  99.         var xhr = s.xhr();
  100.        
  101.         if(!xhr)
  102.         {
  103.             return;
  104.         }
  105.        
  106.         xhr.onreadystatechange = function ()
  107.         {
  108.             if(xhr.readyState === 4)
  109.             {
  110.                 if(s.dataType === "json")
  111.                 {
  112.                     s.success.call(this, JSON.parse(xhr.responseText));
  113.                 }
  114.                 else if(s.dataType === "xml")
  115.                 {
  116.                     s.success.call(this, xhr.responseXML);
  117.                 }
  118.                 else
  119.                 {
  120.                     s.success.call(this, xhr.responseText);
  121.                 }
  122.             }
  123.         }
  124.        
  125.         if (s.username)
  126.         {
  127.             xhr.open(type, s.url, s.async, s.username, s.password);
  128.         }
  129.         else
  130.         {
  131.             xhr.open(type, s.url, s.async);
  132.         }
  133.        
  134.         if (type === "POST")
  135.         {
  136.             xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  137.             xhr.setRequestHeader("Content-length", s.data.length);
  138.             xhr.setRequestHeader("Connection", "close");
  139.         }
  140.        
  141.         try
  142.         {
  143.             xhr.send(s.data === null ? null : s.data);
  144.         }
  145.         catch(sendError)
  146.         {
  147.             Debug.Trace(sendError);
  148.         }
  149.        
  150.         return xhr;
  151.     }
  152. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement