Ajax實(shí)現(xiàn)異步加載數(shù)據(jù)
本文實(shí)例為大家分享了Ajax實(shí)現(xiàn)異步加載數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目結(jié)構(gòu)如下 (需要導(dǎo)入一個(gè)JQuery的包,配置文件web.xml和springmvc-servlet.xml,不在寫(xiě)了,不知道的可以看一下我其它的博客,上邊都有)
異步加載數(shù)據(jù)
首先創(chuàng)建一個(gè)實(shí)體類
package com.zkw.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private String name; private int age; private String sex; }
然后創(chuàng)建一個(gè)Controller
package com.zkw.controller; import com.zkw.pojo.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @RestController public class AjaxController { @RequestMapping("/a2") public List<User> test2(){ List<User> userList = new ArrayList<User>(); userList.add(new User("七七",1,"女")); userList.add(new User("琪琪",1,"女")); userList.add(new User("琦琦",1,"女")); return userList; } }
最后創(chuàng)建一個(gè)jsp頁(yè)面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Ajax異步數(shù)據(jù)加載</title> <script src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.js"></script> <script> $(function () { $("#btn").click(function () { $.post("${pageContext.reques-t.contextPath}/a2",function (data) { var html=""; for (let i = 0; i < data.length; i++){ html +="<tr>" + "<td>" + data[i].name +"</td>"+ "<td>" + data[i].age +"</td>"+ "<td>" + data[i].sex +"</td>"+ "</tr>" } $("#content").html(html); }) }) }) </script> </head> <body> <input type="button" value="加載數(shù)據(jù)" id="btn"> <table> <thead> <tr> <td>姓名</td> <td>年齡</td> <td>性別</td> </tr> </thead> <tbody id="content"></tbody> </table> </body> </html>
結(jié)果如下
用戶登錄的異步驗(yàn)證
先創(chuàng)建一個(gè)Controller
package com.zkw.controller; import com.zkw.pojo.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @RestController public class AjaxController { @RequestMapping("/a3") public String test3(String username,String pwd){ String msg =""; if (username != null){ if (username.equals("admin")){ msg = "ok"; }else{ msg = "用戶名不存在"; } } if (pwd != null){ if (pwd.equals("123456")){ msg = "ok"; }else{ msg = "密碼輸入錯(cuò)誤"; } } return msg; } }
然后創(chuàng)建一個(gè)jsp頁(yè)面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用戶登錄</title> <script src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.js"></script> <script> function a1() { $.post({ url:"${pageContext.request.contextPath}/a3", data:{"username":$("#username").val()}, success(data){ if (data.toString()==="ok"){ $("#userInfo").css("color","green"); }else { $("#userInfo").css("color","red"); } $("#userInfo").html(data); } }) } function a2() { $.post({ url:"${pageContext.request.contextPath}/a3", data:{"pwd":$("#pwd").val()}, success(data){ if (data.toString()==="ok"){ $("#pwdInfo").css("color","green"); }else { $("#pwdInfo").css("color","red"); } $("#pwdInfo").html(data); } }) } </script> </head> <body> <p> 用戶名:<input type="text" id="username" οnblur="a1()"> <span id="userInfo"></span> </p> <p> 密碼名:<input type="password" id="pwd" οnblur="a2()"> <span id="pwdInfo"></span> </p> </body> </html>
結(jié)果如下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決ajax提交到后臺(tái)數(shù)據(jù)成功但返回不走success而走的error問(wèn)題
今天小編就為大家分享一篇解決ajax提交到后臺(tái)數(shù)據(jù)成功但返回不走success而走的error問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08AJAX和WebService實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)具體代碼
AJAX和WebService實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果,在瀏覽網(wǎng)頁(yè)的時(shí)候經(jīng)常會(huì)遇到過(guò),下面與大家分享下具體的實(shí)現(xiàn)步驟2013-05-05pjblog發(fā)表評(píng)論用的ajaxJS.js
pjblog發(fā)表評(píng)論用的ajaxJS.js...2007-04-04Ajax實(shí)現(xiàn)動(dòng)態(tài)顯示并操作表信息的方法
今天小編就為大家分享一篇Ajax實(shí)現(xiàn)動(dòng)態(tài)顯示并操作表信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Ajax+php數(shù)據(jù)交互并且局部刷新頁(yè)面的實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于利用Ajax與php數(shù)據(jù)交互并且局部刷新頁(yè)面的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-07-07利用AjaxSubmit()方法實(shí)現(xiàn)Form提交表單后回調(diào)功能
ajaxSubmit()方法是JQuery Form表單插件中的方法,使用時(shí),需要在jsp或者h(yuǎn)tml頁(yè)面上,引入JQuery庫(kù)和Form插件。接下來(lái)通過(guò)本文給大家分享通過(guò)AjaxSubmit()方法實(shí)現(xiàn)Form提交表單后回調(diào)功能,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05