软件艺术思考者  
混沌,彷徨,立志,蓄势...
公告
日历
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

随笔分类(86)

随笔档案(85)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

树青兄写的一个ajax类。
var http_request = false;
function send_request(method,url,formName,responseType,callback) {
 http_request = false;
 var content =""
 if(forName != null)content = getFormAsString(formName);
  
 if(window.XMLHttpRequest) {
  http_request = new XMLHttpRequest();
  if (http_request.overrideMimeType) {
   http_request.overrideMimeType("text/xml");
  }
 }
 else if (window.ActiveXObject) {
  try {
   http_request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e) {}
  }
 }
 if (!http_request) {
  window.alert("不能创建XMLHttpRequest对象实例.");
  return false;
 }
 if(responseType.toLowerCase()=="text") {
  http_request.onreadystatechange = callback;
 }
 else if(responseType.toLowerCase()=="xml") {
  http_request.onreadystatechange = callback;
 }
 else {
  window.alert("响应类别参数错误。");
  return false;
 }
 if(method.toLowerCase()=="get") {
  http_request.open(method, url, true);
 }
 else if(method.toLowerCase()=="post") {
  http_request.open(method, url, true);
  http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
 }
 else {
  window.alert("http请求类别参数错误。");
  return false;
 }  
 if(window.ActiveXObject) http_request.setRequestHeader("If-Modified-Since", "0");
 http_request.send(content);
}

function getFormAsString(formName){

returnString ="";

formElements=document.forms[formName].elements;

for(var i=formElements.length-1;i>=0; --i ){

returnString+="&"

+escape(formElements[i].name)+"="

+escape(formElements[i].value);

}

}

posted on 2006-11-28 15:03 智者无疆 阅读(299) 评论(4)  编辑  收藏 所属分类: Client teachnolgy research
评论:
  • # re: ajax study (1)  self Posted @ 2006-11-28 15:14
    用法:
    send_request("POST","../index.htm","form1","text",changprocess)  回复  更多评论   

  • # re: ajax study (1)  self Posted @ 2006-12-11 16:19
    改进后的。
    var http_request = false;

    function send_request(method,url,formName,responseType,callback) {
    http_request = false;
    var content =""
    content = getFormAsString(formName);

    if(window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
    http_request.overrideMimeType("text/xml");
    }
    }
    else if (window.ActiveXObject) {
    try {
    http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {}
    }
    }
    if (!http_request) {
    window.alert("不能创建XMLHttpRequest对象实例.");
    return false;
    }
    if(responseType.toLowerCase()=="text") {
    http_request.onreadystatechange = callback;
    }
    else if(responseType.toLowerCase()=="xml") {
    http_request.onreadystatechange = callback;
    }
    else {
    window.alert("响应类别参数错误。");
    return false;
    }
    if(method.toLowerCase()=="get") {
    http_request.open(method, url, true);
    }
    else if(method.toLowerCase()=="post") {
    http_request.open(method, url, true);
    http_request.setRequestHeader('Content-Type','text/xml; charset=utf-8');
    }
    else {
    window.alert("http请求类别参数错误。");
    return false;
    }
    if(window.ActiveXObject) http_request.setRequestHeader("If-Modified-Since", "0");
    http_request.send(content);
    }

    function getFormAsString(formName){

    returnString ="";

    formElements=document.forms[formName].elements;

    for(var i=formElements.length-1;i>=0; --i ){

    returnString+="&"

    +escape(formElements[i].name)+"="

    +escape(formElements[i].value);

    }
    return returnString;
    }   回复  更多评论   

  • # re: ajax study (1)  self Posted @ 2006-12-11 16:20
    一个好东西
    // mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
    // Code licensed under Creative Commons Attribution-ShareAlike License
    // http://creativecommons.org/licenses/by-sa/2.5/
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }

    var oNSResolver = this.createNSResolver(this.documentElement)
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++)
    {
    aResult[i] = aItems.snapshotItem(i);
    }

    return aResult;
    }
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }

    var xItems = this.selectNodes(cXPathString, xNode);
    if( xItems.length > 0 )
    {
    return xItems[0];
    }
    else
    {
    return null;
    }
    }

    Element.prototype.selectNodes = function(cXPathString)
    {
    if(this.ownerDocument.selectNodes)
    {
    return this.ownerDocument.selectNodes(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }

    Element.prototype.selectSingleNode = function(cXPathString)
    {
    if(this.ownerDocument.selectSingleNode)
    {
    return this.ownerDocument.selectSingleNode(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }

    }  回复  更多评论   

  • # re: ajax study (1)  self Posted @ 2006-12-11 16:20
    一个好东西
    // mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
    // Code licensed under Creative Commons Attribution-ShareAlike License
    // http://creativecommons.org/licenses/by-sa/2.5/
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }

    var oNSResolver = this.createNSResolver(this.documentElement)
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++)
    {
    aResult[i] = aItems.snapshotItem(i);
    }

    return aResult;
    }
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }

    var xItems = this.selectNodes(cXPathString, xNode);
    if( xItems.length > 0 )
    {
    return xItems[0];
    }
    else
    {
    return null;
    }
    }

    Element.prototype.selectNodes = function(cXPathString)
    {
    if(this.ownerDocument.selectNodes)
    {
    return this.ownerDocument.selectNodes(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }

    Element.prototype.selectSingleNode = function(cXPathString)
    {
    if(this.ownerDocument.selectSingleNode)
    {
    return this.ownerDocument.selectSingleNode(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }

    }  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 智者无疆 Powered by: 博客园 模板提供:沪江博客


   观音菩萨赞