JS uses Ajax

<script type="text/javascript">
// load function
	function loadName(){
		var xmlHttp = null;
/ / Determine whether the browser preferentially supports XMLHttpRequest
		if(window.XMLHttpRequest){
			xmlHttp = new XMLHttpRequest();
		}else{
/ / Let the old version of IE can also use Ajax
			xmlHttp = new ActiveXObject("Microsoft.XMLHttpRequest");
		}
		//default state
		//alert("ready状态:"+xmlHttp.readyState+";"+"status状态:"+xmlHttp.status);  0    0
		//Request status, the request is divided into four states
//onreadystatechange is a storage function, the onreadystatechange function will be called when the state of the party readyState changes
		xmlHttp.onreadystatechange=function(){
			// request status
//readyState has four states 0 means the request is being initialized 1 and the server establishes the request successfully 2 means the server accepts the request 3 means the server request is being processed 4 means the request is completed and the response is ready
		    if(xmlHttp.readyState==4 && xmlHttp.status==200){ //status 200 means the server response content status OK 404 means the server response content is not found
				//alert("ready状态:"+xmlHttp.readyState+";"+"status状态:"+xmlHttp.status);
				alert(xmlHttp.responseText); //Test the returned data
                               var dataObj = eval("("+xmlHttp.responseText+")"); //Convert the Json object returned by the server to a JS object
//For example, the content returned by the server is {"name":"Zhang San","age":12}
//Then you can use JS objects like this
                           String name = dataObj.name; var age = dataObj.age;
			}
		}
		//get submitted
		//xmlHttp.open("get","getAjax?name=mawanlin&age=12",true);
		//xmlHttp.send();
		//Submit by pose
		xmlHttp.open("post","getAjax",true);
		xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //Indicates submission in form of post
		xmlHttp.send("name=jack&age=11");
	}
</script>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850278&siteId=291194637