Ajax

🔗W3school的Ajax教程

js-Ajax(纯Ajax)

AJAX 不是新的编程语言,而是一种使用现有标准的新方法。
是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。
AJAX = 异步 JavaScript and XML

XMLHttpRequest

所有现代浏览器(IE7+、Firefox、Chrome、Safari 以及 Opera)均支持 XMLHttpRequest 对象,除了IE5 和 IE6 使用 ActiveXObject。
XMLHttpRequest 是 AJAX 的基础,用于在后台与服务器交换数据。

js中创建XMLHttpRequest对象:

var xmlhttp;
if(window.XMLHttpRequest){
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}else{
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

GET与POST请求

方法: open() 、send()、setRequestHeader()

XMLHttpRequest 对象的方法:
在这里插入图片描述
在这里插入图片描述

代码:异步

异步:

  • 无需等待服务器的响应
  • 在等待服务器响应过程中,执行其他脚本;当响应就绪后,则对响应进行处理

示例代码:

// 异步
// 在响应处于就绪状态时, onreadystatechange 事件中的执行的函数
xmlhttp.onreadystatechange=function(){
	if(xmlhttp.readyState==4 && xmlhttp.status==200){
 		document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  	}
}
// async=true
xml.open(method, url, true);
(1)无信息的GET
<html>
<head>
<script type="text/javascript">
function loadXMLDoc(){
	var xmlhttp;
	// 创建MLHttpRequest对象
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}else{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    // 异步
	xmlhttp.onreadystatechange=function(){
  		if(xmlhttp.readyState==4 && xmlhttp.status==200){
    		document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    	}
 	}
 	// 发送部分
	xmlhttp.open("GET","/ajax/demo_get.asp?t=" + Math.random(),true);		// 用随机数向URL添加一个唯一的ID:防止得到的是缓存结果
	xmlhttp.send();
}
</script>
</head>

<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>
(2)有信息的GET

将(1)中的 发送部分 代码改为:

xmlhttp.open("GET","/ajax/demo_get2.asp?fname=Bill&lname=Gates",true);
xmlhttp.send();
(3) 无信息的POST

将(1)中的 发送部分 代码改为:

xmlhttp.open("POST","/ajax/demo_post.asp",true);
xmlhttp.send();
(4) 有信息的POST

将(1)中的 发送部分 代码改为:

xmlhttp.open("POST","/ajax/demo_post2.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Bill&lname=Gates");

代码:同步

同步:

  • JavaScript 会等到服务器响应就绪才能继续执行。如果服务器繁忙或缓慢,应用程序会挂起或停止。

示例代码:

// 同步
// async=false
xml.open(method, url, false);
xml.send();
// 响应部分不在 onreadystatechange 中
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

jQuery-Ajax

编写常规的纯 AJAX 代码并不容易,因为不同的浏览器对 AJAX 的实现并不相同。这意味着您必须编写额外的代码对浏览器进行测试。
jQuery 提供多个与 AJAX 有关的方法。

🔗jQuery 参考手册 - Ajax

.load()

load(url,data,function(response,status,xhr))

在这里插入图片描述
代码示例:

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
      $("#div1").load("/example/jquery/demo_test.txt",function(responseTxt,statusTxt,xhr){
      if(statusTxt=="success")
        alert("外部内容加载成功!");
      if(statusTxt=="error")
        alert("Error: "+xhr.status+": "+xhr.statusText);
    });
  });
});
</script>
</head>
<body>

<div id="div1"><h2>使用 jQuery AJAX 来改变文本</h2></div>
<button>获得外部内容</button>

</body>
</html>

.get()

$.get(URL,callback);
参数说明
必需的 URL请求的 URL
可选的 callback请求成功后所执行的函数名

代码示例:

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.get("/example/jquery/demo_test.asp",function(data,status){
      alert("数据:" + data + "\n状态:" + status);
    });
  });
});
</script>
</head>
<body>

<button>向页面发送 HTTP GET 请求,然后获得返回的结果</button>

</body>
</html>

.post()

$.post(URL,data,callback);
参数说明
必需的 URL请求的 URL
可选的 data连同请求发送的数据
可选的 callback请求成功后所执行的函数名

代码示例:

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.post("/example/jquery/demo_test_post.asp",
    {
      name:"Donald Duck",
      city:"Duckburg"
    },
    function(data,status){
      alert("数据:" + data + "\n状态:" + status);
    });
  });
});
</script>
</head>
<body>

<button>向页面发送 HTTP POST 请求,并获得返回的结果</button>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值