批改状态:合格
老师批语:完成的很好, 一二阶段作业务必在6月10日前完成
一、Ajax跨域请求分为以下7种
load()请求数据$.get()请求数据$.post()请求数据$.getJSON()请求JSON数据$.ajax()请求数据$.ajax()-jsonp-跨域请求数据$.ajax()-jsonp-跨域请求数据
代码演示:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><script src="lib/jquery-3.5.1.js"></script><title>Ajax</title><style>body {width: 200px;display: grid;gap: 15px;}button {text-align: left;height: 26px;}button.hover {background-color: #ddd;cursor: pointer;}</style></head><body><button type="button">1.load()请求数据</button><button type="button">2.$.get()请求数据</button><button type="button">3.$.post()请求数据</button><button type="button">4.$.getJSON()请求JSON数据</button><button type="button">5.$.ajax()请求数据</button><button type="button">6.$.ajax()-jsonp-跨域请求数据1</button><button type="button">7.$.ajax()-jsonp-跨域请求数据2</button></body><script>// 1.load()请求数据 :加载html片断$("button:first-of-type").click(function () {// console.log($(this));$(this).after("<div>").next().load("nav.html");});//2.$.get():以get方式从服务器获取资源/数据$("button:nth-of-type(2)").click(function (ev) {// console.log($(this));// $.get(url,data,callback)$.get("users.php", { id: 2 }, function (data) {// console.log(data);$(ev.target).after("<div>").next().html(data);});});//3.$.post():以post方式从服务器获取资源/数据$("button:nth-of-type(3)").click(function (ev) {// console.log($(this));// $.post(url,data,callback)$.post("users.php", { id: 4 }, function (data) {// console.log(data);$(ev.target).after("<div>").next().html(data);});});//4.$.getJSON():接口返回的大多是JSON$("button:nth-of-type(3)").click(function (ev) {// console.log($(this));// $.get(url,data,callback)$.getJSON("users.php?id=2", function (data) {//返回的就是JS对象,不必再转换//console.log(JSON,parse(data));console.log(data);var res = data.id + ":" + data.name + "," + data.age + "岁";$(ev.target).after("<div>").next().html(data);});});// 5. $.ajax(): 终级方法,该方法的参数是一个对象自变量,前面4种方法可以放弃,只需要掌握这一个方法,方便快捷。// $.ajax({// //请求类型// type: "GET",// //请求URL// url: url,// //发送的数据// data: data,// //期望服务器返回/响应的数据类型// dataType: "json",// //成功时的回调函数// success: callback,// });$("button:nth-of-type(5)").click(function (ev) {$.ajax({type: "GET",url: "users.php",data: { id: 3 },dataType: "html",success: function (data) {$(ev.target).after("<div>").next().html(data);},});});//6.$.ajax()-jsonp-1:跨域请求数据$("button:nth-of-type(6)").click(function (ev) {$.ajax({type: "GET",url: "http://php.edu/0522/test2.php?jsonp=?&id=1",dataType: "jsonp",success: function (data) {console.log(data);var data = JSON.parse(data);console.log(data);var data ="<p>" +data.title +"</p><p>" +data.user.name +", 邮箱:" +data.user.email +"</p>";$(ev.target).after("<div>").next().html(data);},});});//7.$.ajax()-jsonp-2:跨域请求数据$("button:last-of-type").click(function (ev) {$.ajax({type: "GET",url: "http://php.edu/0522/test2.php?jsonp=?&id=2",dataType: "jsonp",jsonpCallback: "handle",});});function handle(data) {console.log(data);var data = JSON.parse(data);console.log(data);var data ="<p>" +data.title +"</p><p>" +data.user.name +", 邮箱:" +data.user.email +"</p>";$("button:last-of-type").after("<div>").next().html(data);}</script></html>
输出效果:
二、Ajax无刷新分页技术
代码演示
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><script src="lib/jquery-3.5.1.js"></script><title>Ajax无刷新分页技术</title><style>table {border-collapse: collapse;border: 1px solid;text-align: center;margin: auto;width: 500px;}table caption {font-size: 1.2rem;margin-bottom: 10px;}th,td {border: 1px solid;padding: 5px;}thead tr:first-of-type {background-color: rgb(70, 233, 245);}p {text-align: center;}p a {text-decoration: none;border: 1px solid;padding: 0 8px;}.active {background-color: rgb(70, 233, 245);}</style></head><body><table><caption>员工信息表</caption><thead><tr><th>id</th><th>姓名</th><th>年龄</th><th>性别</th><th>职位</th><th>手机号</th></tr></thead><tbody></tbody></table><!-- 分页条 --><p></p></body><script>// 默认是第1页var page = 1;// 默认显示第一页,用一个函数来实现显示getPageData(page);// 分页函数function getPageData(page) {// ajax无刷新分页$.ajax({type: "post",url: "page_data.php",data: { page: page },dataType: "json",success: show,});}// 数据显示函数function show(data) {// 1. 显示表格数据console.log(data);console.log(data.pages);console.log(data.staffs);var str = "";data.staffs.forEach(function (staff) {str += "<tr>";str += "<td>" + staff.id + "</td>";str += "<td>" + staff.name + "</td>";str += "<td>" + staff.age + "</td>";str += "<td>" + staff.sex + "</td>";str += "<td>" + staff.position + "</td>";str += "<td>" + staff.mobile + "</td>";str += "</tr>";});// $("tbody").append(str);$("tbody").html(str);// 2. 显示分页条var str = "";for (var i = 1; i <= data.pages; i++) {// $("<a>").attr("href", "").attr("data-index", i).html(i).appendTo("p");str += '<a href="" data-index=' + i + ">" + i + "</a>";}// 将页码添到分页条, 并将第一个设置为高亮$("p").html(str).find("a").first().addClass("active");//将页码添到分页条, 并将最后一个设置为高亮 $("p").html(str).find("a").last().addClass("active");// 将当前页码设置为高亮$("p").html(str).find("a").eq(page - 1).addClass("active");// 分页条的点击事件$("p a").click(function (ev) {// 禁用<a>的跳转默认行为ev.preventDefault();var page = $(this).attr("data-index");$("tbody").html("");getPageData(page);});}</script></html>
输出效果:
三、总结
1.代码执行过程中,使用Chrome浏览器预览时Ajax请求出现“Method Not Allowed 405”错误,表格数据无法加载,而firefox浏览器显示正常。百度到的解决方法:
打开Chrome快捷方式的属性中设置:
右击Chrome浏览器快捷方式,选择“属性”,
在“目标”中加上”—allow-file-access-from-files”,注意前面有个空格,
重启Chrome浏览器便可。
2.写代码的过程中一定要添加注释,注释是后期进行数据维护的依据。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号