码迷,mamicode.com
首页 > 其他好文 > 详细

ajax封装

时间:2014-09-27 09:32:19      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   ar   for   sp   on   c   cti   

function ajaxRequest(url, params, processor, async) {
  url = url || ‘‘;
  async = async || false;
  params = params || {};

  var req = null;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  var tmp = "";
  for (var i in params) {
    tmp = tmp + i + "=" + params[i] + "&";
  }
//alert(tmp);
  if (req) {
    req.open("GET", url + "?" + tmp, async);//true,ajax false异步、true同步
    req.onreadystatechange = function() {
    if (req.readyState == 4 && req.status == 200) {
      var result = req.responseText;
//document.body.innerHTML = document.body.innerHTML + result;
       if (processor) {
        processor(result);
      }
    }
  };
  req.send(null);
  }
}

ajax封装

标签:http   io   os   ar   for   sp   on   c   cti   

原文地址:http://www.cnblogs.com/liyong0125/p/3995946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!