首页 > Web开发 > 详细

原生的ajax(json)

时间:2015-03-25 16:42:09      阅读:313      评论:0      收藏:0      [点我收藏+]
function getXHR(){
			if(window.XMLHttpRequest){
				return new XMLHttpRequest();
			}else{
				try{
					return new ActiveXObject("Msxml2.XMLHTTP.6.0");
				}catch(e1){
					try{
						return new ActiveXObject("Msxml2.XMLHTTP.3.0");
					}catch(e2){
						throw new Error("xmt is not support!");
					}
				}
			}
		}

		function XHR(){
			this.xhr=getXHR();
		}
		XHR.prototype.getJsonByGet=function(url){
			var XHRequest=this.xhr;
			XHRequest.open("GET",url);
			XHRequest.onreadystatechange=function(){
				if(XHRequest.readyState===4 && XHRequest.status===200){
					var type=XHRequest.getResponseHead("Content-Type");
					if(type.match(/^text/))
						return JSON.parse(XHRequest.responseText);
				}
			}
			XHRequest.send(null);
		}
		XHR.prototype.getJsonByPost=function(url,postJson){
			var XHRequest=this.xhr;
			XHRequest.open("POST",url);
			XHRequest.setRequestHeader("Content-Type","text/plain;charset=UTF-8");
			XHRequest.onreadystatechange=function(){
				if(XHRequest.readyState===4 && XHRequest.status===200){
					var type=XHRequest.getResponseHead("Content-Type");
					if(type.match(/^text/))
						return JSON.parse(XHRequest.responseText);
				}
			}
			XHRequest.send(JSON.stringify(postJson));
		}

  

原生的ajax(json)

原文:http://www.cnblogs.com/dunken/p/4365794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!