局部刷新页面的Ajax

Ajax从零开始(一)

#记录Ajax-day01-01~07

基本步骤-代码

    //创建ajax对象
	var xhr = new XMLHttpRequest();
	//2、告诉Ajax对象要向啊发送请求,以什么方式发送请求
	//参数:1)请求方式 2)请求地址
	xhr.open('get',"test.txt","true");
	//发送请求
	xhr.send();
	//获取服务器端响应到客户端的数据
	xhr.onload = function () {
	console.log(xhr.responseText);
	}

open():.open( 提交方式 提交地址 是否异步)

1、提交方式 --get(代码如上)

服务端响应的数据格式:服务器端大多数情况下会以JSON对象作为响应数据格式。当客户端拿到响应数据时,要将JSON数据和HTML字符串进行拼接,然后将拼接的结果展示在页面中。(代码如下)

<script type="text/javascript">
			//创建ajax对象
			var xhr = new XMLHttpRequest();
			//2、告诉Ajax对象要向啊发送请求,以什么方式发送请求
			//参数:1)请求方式 2)请求地址
			//xhr.open('get',"test.json","true");
			xhr.open('get','http://localhost/ajaxDemo/test.json');
			//发送请求
			xhr.send();
			//获取服务器端响应到客户端的数据
			xhr.onload = function () {
				//console.log(xhr.responseText);
				var responseText = JSON.parse(xhr.responseText);
				console.log(responseText);
				var str = '<h2>'+responseText.name+'</h2>';
				document.body.innerHTML = str;
			}
</script>

补充:请求参数传递:
xhr.open(‘get’,‘http://localhost/ajaxDemo/index06.json?’+params);

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<p>
			<input type="text" name="" id="username" value="" />
		</p>
		<p>
			<input type="text" name="" id="age" value="" />
		</p>
		<p>
			<input type="button" name="" id="btn" value="提交" />
		</p>
		<script type="text/javascript">
			var btn = document.getElementById('btn');
			var username = document.getElementById('username');
			var age = document.getElementById('age');
			btn.onclick = function(){
				//创建ajac对象
				var xhr = new XMLHttpRequest();
				//获取用户在文本框中输入的值
				var nameValue = username.value;
				var ageValue = age.value;
				//拼接请求参数
				var params = 'username=' + nameValue + '&age=' + ageValue;
				//配置Ajax对象------ ?+params
				xhr.open('get','http://localhost/ajaxDemo/index06.json?'+params);
				//发送请求
				xhr.send();
				//获取服务端响应的数据
				xhr.onload = function(){
					console.log(xhr.responseText);
				}
			}
		</script>
	</body>
</html>

1、提交方式 --post:

	xhr.open('post','http://localhost/ajaxDemo/index06.json');
	//设置请求参数格式类型(必须要)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
	//发送请求
	xhr.send(params);
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<p>
			<input type="text" name="" id="username" value="" />
		</p>
		<p>
			<input type="text" name="" id="age" value="" />
		</p>
		<p>
			<input type="button" name="" id="btn" value="提交" />
		</p>
		<script type="text/javascript">
			var btn = document.getElementById('btn');
			var username = document.getElementById('username');
			var age = document.getElementById('age');
			btn.onclick = function(){
				//创建ajac对象
				var xhr = new XMLHttpRequest();
				//获取用户在文本框中输入的值
				var nameValue = username.value;
				var ageValue = age.value;
				//拼接请求参数
				var params = 'username=' + nameValue + '&age=' + ageValue;
				//配置Ajax对象------ ?+params
				xhr.open('post','http://localhost/ajaxDemo/index06.json');
				//设置请求参数格式类型(必须要)
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
				//发送请求
				xhr.send(params);
				//获取服务端响应的数据
				xhr.onload = function(){
					console.log(xhr.responseText);
				}
			}
		</script>
	</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值