XMLHttpRequest v1.1

/**/ /*

Cross-Browser XMLHttpRequest v1.1
=================================

Emulate Gecko 'XMLHttpRequest()' functionality in IE and Opera. Opera requires
the Sun Java Runtime Environment <http://www.java.com/>.

by Andrew Gregory
http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/

This work is licensed under the Creative Commons Attribution License. To view a
copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send
a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
USA.

Not Supported in Opera
----------------------
* user/password authentication
* responseXML data member

Not Fully Supported in Opera
----------------------------
* async requests
* abort()
* getAllResponseHeaders(), getAllResponseHeader(header)

*/

//  IE support
if  (window.ActiveXObject  &&   ! window.XMLHttpRequest)  {
  window.XMLHttpRequest 
= function() {
    
return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
  }
;
}

//  Gecko support
/**/ /* ;-) */
//  Opera support
global_fakeOperaXMLHttpRequestSupport  =   false ;
// if (window.opera && !window.XMLHttpRequest) { THIS IS COMMENTED OUT BECAUSE OPERA 8 has window.XMLHttpRequest but still does not support setRequestHeader
if  (window.opera)  {
    global_fakeOperaXMLHttpRequestSupport 
= true;
  window.XMLHttpRequest 
= function() {
    
this.readyState = 0// 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete
    this.status = 0// HTTP status codes
    this.statusText = '';
    
this._headers = [];
    
this._aborted = false;
    
this._async = true;
    
this.abort = function() {
      
this._aborted = true;
    }
;
    
this.getAllResponseHeaders = function() {
      
return this.getAllResponseHeader('*');
    }
;
    
this.getAllResponseHeader = function(header) {
      
var ret = '';
      
for (var i = 0; i < this._headers.length; i++{
        
if (header == '*|| this._headers[i].h == header) {
          ret 
+= this._headers[i].h + ': ' + this._headers[i].v + '\n';
        }

      }

      
return ret;
    }
;
    
this.setRequestHeader = function(header, value) {
      
this._headers[this._headers.length] = {h:header, v:value};
    }
;
    
this.open = function(method, url, async, user, password) {
        
        
//alert(method+"]["+url+"]["+async+"]["+user+""+""+"");
      this.method = method;
      
this.url = url;
      
this._async = true;
      
this._aborted = false;
      
if (arguments.length >= 3{
        
this._async = async;
      }

      
if (arguments.length > 3{
        
// user/password support requires a custom Authenticator class
        opera.postError('XMLHttpRequest.open() - user/password not supported');
      }

      
this._headers = [];
      
this.readyState = 1;
      
if (this.onreadystatechange) {
        
this.onreadystatechange();
      }

    }
;
    
this.send = function(data) {
      
if (!navigator.javaEnabled()) {
        alert(
"XMLHttpRequest.send() - Java must be installed and enabled.");
        
return;
      }

      
if (this._async) {
        setTimeout(
this._sendasync, 0this, data);
        
// this is not really asynchronous and won't execute until the current
        // execution context ends
      }
 else {
        
this._sendsync(data);
      }

    }

    
this._sendasync = function(req, data) {
      
if (!req._aborted) {
        req._sendsync(data);
      }

    }
;
    
this._sendsync = function(data) {
      
this.readyState = 2;
      
if (this.onreadystatechange) {
        
this.onreadystatechange();
      }

      
// open connection
      var url = new java.net.URL(new java.net.URL(window.location.href), this.url);
      
var conn = url.openConnection();
      
for (var i = 0; i < this._headers.length; i++{
        conn.setRequestProperty(
this._headers[i].h, this._headers[i].v);
      }

      
this._headers = [];
      
if (this.method == 'POST') {
        
// POST data
        conn.setDoOutput(true);
        
var wr = new java.io.OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        wr.close();
      }

      
// read response headers
      // NOTE: the getHeaderField() methods always return nulls for me :(
      var gotContentEncoding = false;
      
var gotContentLength = false;
      
var gotContentType = false;
      
var gotDate = false;
      
var gotExpiration = false;
      
var gotLastModified = false;
      
for (var i = 0; ; i++{
        
var hdrName = conn.getHeaderFieldKey(i);
Outlining
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值