【JAVAEE——Ajax】

一:Ajax概述

1.1什么是同步,什么是异步

同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待    卡死状态

异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都可以随    意做其他事情,不会被卡死

 

1.2Ajax的运行原理

页面发起请求,会将请求发送给浏览器内核中的Ajax引擎,Ajax引擎会提交请求到 服务器端,在这段时间里,客户端可以任意进行任意操作,直到服务器端将数据返回 给Ajax引擎后,会触发你设置的事件,从而执行自定义的js逻辑代码完成某种页面1      功能。

二:js原生的Ajax技术

js原生的Ajax其实就是围绕浏览器内内置的Ajax引擎对象进行的,要使用js原生的Ajax完成异步操作,有如下几个步骤:

  1. 创建Ajax引擎对象
  2. 为Ajax引擎对象绑定监听(监听服务器已将数据响应给引擎)
  3. 绑定提交地址
  4. 发送请求
  5. 接受响应数据
<script type="text/javascript">
    function fn1(){
    	//1.创建ajax引擎对象
    	var xmlHttp = new XMLHttpRequest();
    	//2.绑定监听
    	xmlHttp.onreadystatechange=function(){
    		//5.接收响应数据
    		if(xmlHttp.readyState==4&&xmlHttp.status==200){
    		var res = xmlHttp.responseText;
    		document.getElementById("span1").innerHTML=res;
    		}
    	}
    	//3.绑定地址
    	xmlHttp.open("GET", "${pageContext.request.contextPath}/ajax?name=lx", true);
    	//4.发送请求
    	xmlHttp.send();
    	
    }
    function fn2(){
    	//1.创建ajax引擎对象
    	var xmlHttp = new XMLHttpRequest();
    	//2.绑定监听
    	xmlHttp.onreadystatechange=function(){
    		//5.接收响应数据
    		if(xmlHttp.readyState==4&&xmlHttp.status==200){
    		var res = xmlHttp.responseText;
    		document.getElementById("span2").innerHTML=res;
    		}
    	}
    	//3.绑定地址
    	xmlHttp.open("POST", "${pageContext.request.contextPath}/ajax", false);
    	//4.发送请求
    	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	xmlHttp.send("name=lx");
    	
    }
</script>
</head>
<body>
    <input type="button" value="异步访问" onclick="fn1()" /><span id="span1"></span><br>
    <input type="button" value="同步访问" onclick="fn2()" /><span id="span2"></span><br>
    <input type="button" value="测试" onclick="alert()">   
</body>

三:Jquery的Ajax技术 

jquery是一个优秀的js框架,自然对js原生的ajax进行了封装,封装后的ajax的操   作方法更简洁,功能更强大,与ajax操作相关的jquery方法有如下几种,但开发中  经常使用的有三种

<script type="text/javascript">

	function fn1() {
        //get异步访问
		$.get(
			"${pageContext.request.contextPath}/ajax2", //url
			{                                          //data
			name : "账单",
			age : 12
		     }, 
		function(data) {                               //callback
			alert(data.name);
		},
		"json"                                         //type
		     );
	}

	function fn2() {
		//post异步访问
		$.post(
			"${pageContext.request.contextPath}/ajax2", //url
			{                                          //data
			name : "账单",
			age : 12
		     }, 
		function(data) {                               //callback
			alert(data.name);
		},
		"json"                                         //type
		     );
	}
	
	
	function fn3() {
		//ajax
		$.ajax({
			type : "POST",
			async : true,
			url : "${pageContext.request.contextPath}/ajax2",
			data : {
				"name" : "类型",
				"age" : 21
			},
			success : function(msg) {
				alert(msg.name);
			},
			error : function(msg) {
				alert("失败");
			},
			dataType : "json"
		});
	}
</script>
</head>
<body>
     <input type="button" value="get访问" onclick="fn1()" /><span id="span1"></span><br>
     <input type="button" value="post访问" onclick="fn2()" /><span id="span2"></span><br>
     <input type="button" value="ajax访问" onclick="fn3()" /><span id="span3"></span>
</body>

猜你喜欢

转载自blog.csdn.net/bigLiu66/article/details/84934893