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,不在寫了,不知道的可以看一下我其它的博客,上邊都有)

異步加載數(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 對(duì)象 包含post和get兩種異步傳輸方式
Ajax對(duì)象接受一個(gè)對(duì)象字面量為參數(shù),這個(gè)對(duì)象字面量中包含method,url,success,params,fail參數(shù)2009-07-07
jquery中的ajax如何返回結(jié)果而非回調(diào)方式即為同順序執(zhí)行
默認(rèn)ajax是異步的,也就是在未響應(yīng)到結(jié)果時(shí)不影響向下的執(zhí)行,如果非要返回結(jié)果的話,將ajax 中的參數(shù) async 改為 false,即為同順序執(zhí)行2014-05-05
Select2在使用ajax獲取遠(yuǎn)程數(shù)據(jù)時(shí)顯示默認(rèn)數(shù)據(jù)的方法
今天小編就為大家分享一篇Select2在使用ajax獲取遠(yuǎn)程數(shù)據(jù)時(shí)顯示默認(rèn)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-08-08
ajax獲取php頁(yè)面的返回參數(shù),控件賦值的方法
下面小編就為大家?guī)?lái)一篇ajax獲取php頁(yè)面的返回參數(shù),控件賦值的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-10-10

