最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ajax實(shí)現(xiàn)異步加載數(shù)據(jù)

 更新時(shí)間:2021年11月16日 17:28:03   作者:起名好難呀!  
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)異步加載數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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ì)象 包含post和get兩種異步傳輸方式

    Ajax對(duì)象接受一個(gè)對(duì)象字面量為參數(shù),這個(gè)對(duì)象字面量中包含method,url,success,params,fail參數(shù)
    2009-07-07
  • 淺談Bootstrap的DatePicker日期范圍選擇

    淺談Bootstrap的DatePicker日期范圍選擇

    下面小編就為大家?guī)?lái)一篇淺談Bootstrap的DatePicker日期范圍選擇。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • jquery中的ajax如何返回結(jié)果而非回調(diào)方式即為同順序執(zhí)行

    jquery中的ajax如何返回結(jié)果而非回調(diào)方式即為同順序執(zhí)行

    默認(rèn)ajax是異步的,也就是在未響應(yīng)到結(jié)果時(shí)不影響向下的執(zhí)行,如果非要返回結(jié)果的話,將ajax 中的參數(shù) async 改為 false,即為同順序執(zhí)行
    2014-05-05
  • 深入理解ajax系列第一篇之XHR對(duì)象

    深入理解ajax系列第一篇之XHR對(duì)象

    ajax是asynchronous javascript and XML的簡(jiǎn)寫,中文翻譯是異步的javascript和XML,這一技術(shù)能夠向服務(wù)器請(qǐng)求額外的數(shù)據(jù)而無(wú)須卸載頁(yè)面,會(huì)帶來(lái)更好的用戶體驗(yàn)
    2016-11-11
  • Select2在使用ajax獲取遠(yuǎn)程數(shù)據(jù)時(shí)顯示默認(rèn)數(shù)據(jù)的方法

    Select2在使用ajax獲取遠(yuǎn)程數(shù)據(jù)時(shí)顯示默認(rèn)數(shù)據(jù)的方法

    今天小編就為大家分享一篇Select2在使用ajax獲取遠(yuǎn)程數(shù)據(jù)時(shí)顯示默認(rèn)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • ajax獲取php頁(yè)面的返回參數(shù),控件賦值的方法

    ajax獲取php頁(yè)面的返回參數(shù),控件賦值的方法

    下面小編就為大家?guī)?lái)一篇ajax獲取php頁(yè)面的返回參數(shù),控件賦值的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • Ajax初試之讀取數(shù)據(jù)篇

    Ajax初試之讀取數(shù)據(jù)篇

    上次我們講了"ajax開(kāi)始準(zhǔn)備篇",做好了基本的ajax準(zhǔn)備工作以后.我們開(kāi)始牛刀小試一下:ajax初試之讀取數(shù)據(jù)篇.是的,今天我們要實(shí)現(xiàn)的效果是.在不刷新網(wǎng)頁(yè)的情況下讀取并顯示服務(wù)端的數(shù)據(jù).
    2010-08-08
  • asp簡(jiǎn)單的ajax留言板(采用三層模式)

    asp簡(jiǎn)單的ajax留言板(采用三層模式)

    asp簡(jiǎn)單的ajax留言板(采用三層模式)...
    2006-10-10
  • Ajax教程實(shí)例詳解

    Ajax教程實(shí)例詳解

    AJAX 是與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁(yè)的藝術(shù),在不重新加載整個(gè)頁(yè)面的情況下。接下來(lái)通過(guò)本文給大家介紹Ajax教程實(shí)例詳解,對(duì)ajax相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • Jquery中$.ajax()方法參數(shù)詳解

    Jquery中$.ajax()方法參數(shù)詳解

    本文是小編日常整理些有關(guān)jquery ajax()參數(shù)詳解,由于jquery ajax方法參數(shù)比較多,靠大腦記住有點(diǎn)犯難,下面小編把內(nèi)容整理分享到腳本之家網(wǎng)站,供大家參考
    2015-10-10

最新評(píng)論

房山区| 辽宁省| 通化县| 永城市| 泗水县| 宣城市| 青浦区| 班戈县| 隆林| 池州市| 民丰县| 镇远县| 延津县| 河北省| 咸宁市| 肥西县| 读书| 博白县| 黑龙江省| 张北县| 肇庆市| 五家渠市| 博白县| 阿瓦提县| 上虞市| 阿拉尔市| 乐山市| 虹口区| 禹城市| 镇雄县| 通河县| 洮南市| 礼泉县| 龙门县| 梧州市| 靖西县| 沐川县| 彭州市| 邛崃市| 冷水江市| 安陆市|