关于AJAX的一些介绍

一、名称
异步的javascript and xml
二、原理
1、通过 XMLHttpRequest 与服务器交换数据
2、服务数据通过json 或者 xml格式返回
3、浏览器端通过js+ccs渲染展示数据
三、get请求

		<button type="button" id=btn>发送</button>
		<p id="zoon"></p>
		<script type="text/javascript">
			btn.onclick=function(){
				//创建xhr
				var xhr=new XMLHttpRequest();
				//打开链接
				xhr.open("GET",'zen.txt',false);
				//监听事件  readyState状态码  statu由服务器返回的状态行的值。
				xhr.onreadystatechange=function(){
					if(xhr.status==200&&xhr.readyState==4){
						zoon.innerHTML=xhr.responseText;
					}
				}
				//发送
				xhr.send();
			}
		</script>

四、post请求

input type="text" id="in1" value="" placeholder="请输入用户名"/>
		<input type="text" id="in2" value="" placeholder="请输入年龄"/>
		<button type="button" id="btn">发送</button>
		<p id="zoon"></p>
		<script type="text/javascript">
			btn.onclick=function(){
				//创建xhr链接
			var xhr=new XMLHttpRequest();
			var name=document.getElementById("in1").value;
			console.log(name)
			var age=document.getElementById('in2').value;
			var zoon=document.getElementById('zoon');
			//打开
			xhr.open('POST','http://localhost/echo.php',false);
			//监听事件
			xhr.onreadystatechange=function(){
				if(xhr.status==200&&xhr.readyState==4){
					zoon.innerHTML=xhr.responseText;
				}
			}
			// 设置请求头格式
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			//发送
			xhr.send(`name=${name}&age=${age}`);
			}
			
		</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值