利用Http请求 获取数据

来源:互联网 发布:linux远程重启服务器 编辑:程序博客网 时间:2024/05/16 00:32

 

 

 

 

利用Http请求 获取数据

 

 

  1. /* 
  2.     XmlHttpRequest Wrapper
  3.     Version 1.2.2
  4.     29 Jul 2005 
  5.     adamv.com/dev/
  6. */
  7. var Http = {
  8.     ReadyState: {
  9.         Uninitialized: 0,
  10.         Loading: 1,
  11.         Loaded:2,
  12.         Interactive:3,
  13.         Complete: 4
  14.     },
  15.         
  16.     Status: {
  17.         OK: 200,
  18.         
  19.         Created: 201,
  20.         Accepted: 202,
  21.         NoContent: 204,
  22.         
  23.         BadRequest: 400,
  24.         Forbidden: 403,
  25.         NotFound: 404,
  26.         Gone: 410,
  27.         
  28.         ServerError: 500
  29.     },
  30.         
  31.     Cache: {
  32.         Get: 1,
  33.         GetCache: 2,
  34.         GetNoCache: 3,
  35.         FromCache: 4
  36.     },
  37.     
  38.     Method: {Get: "GET", Post: "POST", Put: "PUT", Delete: "DELETE"},
  39.     
  40.     enabled: false,
  41.     logging: false,
  42.     _get: null// Reference to the XmlHttpRequest object
  43.     _cache: new Object(),
  44.     
  45.     Init: function(){
  46.         Http._get = Http._getXmlHttp()
  47.         Http.enabled = (Http._get != null)
  48.         Http.logging = (window.Logging != null);
  49.     },
  50.     
  51.     _getXmlHttp: function(){
  52.     /*@cc_on @*//*@if (@_jscript_version >= 5)
  53.         try { return new ActiveXObject("Msxml2.XMLHTTP"); } 
  54.         catch (e) {} 
  55.         try { return new ActiveXObject("Microsoft.XMLHTTP"); } 
  56.         catch (e) {} 
  57.     @end @*/
  58.         try { return new XMLHttpRequest();}
  59.         catch (e) {}
  60.         return null;
  61.     },
  62. /*
  63.     Params:
  64.         url: The URL to request. Required.
  65.         cache: Cache control. Defaults to Cache.Get.
  66.         callback: onreadystatechange function, called when request is completed. Optional.
  67.         method: HTTP method. Defaults to Method.Get.
  68. */
  69.     get: function(params, callback_args){   
  70.         if (!Http.enabled) throw "Http: XmlHttpRequest not available.";
  71.         
  72.         var url = params.url;
  73.         if (!url) throw "Http: A URL must be specified";
  74.                 
  75.         var cache = params.cache || Http.Cache.Get;
  76.         var method = params.method || Http.Method.Get;
  77.         var callback = params.callback;
  78.         
  79.         if ((cache == Http.Cache.FromCache) || (cache == Http.Cache.GetCache))
  80.         {
  81.             var in_cache = Http.from_cache(url, callback, callback_args)
  82.             if (Http.logging){
  83.                 Logging.log(["Http: URL in cache: " + in_cache]);
  84.             }
  85.             if (in_cache || (cache == Http.Cache.FromCache)) return in_cache;
  86.         }
  87.         
  88.         if (cache == Http.Cache.GetNoCache)
  89.         {
  90.             var sep = (-1 < url.indexOf("?")) ? "&" : "?"   
  91.             url = url + sep + "__=" + encodeURIComponent((new Date()).getTime());
  92.         }
  93.     
  94.         // Only one request at a time, please
  95.         if ((Http._get.readyState != Http.ReadyState.Uninitialized) && 
  96.             (Http._get.readyState != Http.ReadyState.Complete)){
  97.             this._get.abort();
  98.             
  99.             if (Http.logging){
  100.                 Logging.log(["Http: Aborted request in progress."]);
  101.             }
  102.         }
  103.         
  104.         Http._get.open(method, url, true);
  105.         Http._get.onreadystatechange =  function() {
  106.             if (Http._get.readyState != Http.ReadyState.Complete) return;
  107.             
  108.             if (Http.logging){
  109.                 Logging.log(["Http: Returned, status: " + Http._get.status]);
  110.             }
  111.             if ((cache == Http.Cache.GetCache) && (Http._get.status == Http.Status.OK)){
  112.                 Http._cache[url] = Http._get.responseText;
  113.             }
  114.             
  115.             if (callback_args == null) callback_args = new Array();
  116.             var cb_params = new Array();
  117.             cb_params.push(Http._get);
  118.             for(var i=0;i<callback_args.length;i++)
  119.                 cb_params.push(callback_args[i]);
  120.                 
  121.             callback.apply(null, cb_params);
  122.         }
  123.         
  124.         if(Http.logging){
  125.             Logging.log(["Http: Started/n/tURL: " + url + "/n/tMethod: " + method + "; Cache: " + Hash.keyName(Http.Cache,cache)])
  126.         }
  127.         
  128.         Http._get.send(params.body || null);
  129.     },
  130.     
  131.     from_cache: function(url, callback, callback_args){
  132.         var result = Http._cache[url];
  133.         
  134.         if (result != null) {
  135.             var response = new Http.CachedResponse(result)
  136.             
  137.             var cb_params = new Array();
  138.             cb_params.push(response);
  139.             for(var i=0;i<callback_args.length;i++)
  140.                 cb_params.push(callback_args[i]);
  141.                             
  142.             callback.apply(null, cb_params);
  143.                 
  144.             return true
  145.         }
  146.         else
  147.             return false
  148.     },
  149.     
  150.     clear_cache: function(){
  151.         Http._cache = new Object();
  152.     },
  153.     
  154.     is_cached: function(url){
  155.         return Http._cache[url]!=null;
  156.     },
  157.     
  158.     CachedResponse: function(response) {
  159.         this.readyState = Http.ReadyState.Complete
  160.         this.status = Http.Status.OK
  161.         this.responseText = response
  162.     }   
  163. }
  164. Http.Init()
  165. function json_response(response){
  166.     var js = response.responseText;
  167.     try{
  168.         return eval(js); 
  169.     } catch(e){
  170.         if (Http.logging){
  171.             Logging.logError(["json_response: " + e]);
  172.         }
  173.         else{
  174.             alert("Error: " + e + "/n" + js);
  175.         }
  176.         return null;
  177.     }
  178. }
  179. function getResponseProps(response, header){
  180.     try {
  181.         var s = response.getResponseHeader(header || 'X-Ajax-Props');
  182.         if (s==null || s=="")
  183.             return new Object()
  184.         else
  185.             return eval("o="+s)
  186.     } catch (e) { return new Object() }
  187. }
  188. function Geturl(AssID,ActiveID) 
  189.         {   
  190.             try
  191.             {
  192.                 var strpath="../redirect.ashx?;
  193.                 Http.get({
  194.                 url: strpath,
  195.                 callback: getHttpContents,cache: 
  196.                 Http.Cache.GetNoCache});
  197.             }
  198.             catch(e)
  199.             {
  200.                 alert(e.name+":"+e.message);
  201.             }                   
  202.             
  203.         }
  204.         
  205.         // get the result
  206.         function getHttpContents(result){
  207.         
  208.             if (result.status==Http.Status.OK)
  209.             {   
  210.                 alert(result.responseText);         
  211.                 initPlayerByUrl(result.responseText);           
  212.                 setPlay();
  213.             } 
  214.             else 
  215.             {           
  216.                 alert("can't get ");
  217.             }
  218.         }
function jb()
{
var A=null;
try
{
A=new ActiveXObject("Msxml2.XMLHTTP")
} catch(e) {
try
{
A=new ActiveXObject("Microsoft.XMLHTTP")
} catch(oc) {
A=null
}
}
if ( !A && typeof XMLHttpRequest != "undefined" )
{
A=new XMLHttpRequest()
}
return A
}

這段是從Google Suggest的js中擷取出來的,
如此一來,只要將程式改成

var xmlDom = jb();
var strData = "code=123";
xmlDom.open("POST","default.asp",false);
xmlDom.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
xmlDom.send(strData);

就可以了,

至於, new ActiveXObject("Microsoft.XMLHTTP") 跟 new XMLHttpRequest()
在功能上有沒有什麼差異,這就要等你去發掘了,
因為我的問題,到這已經算是解決了...

原创粉丝点击