在ajax中如何使用jquery循环。

假设result的数据是这个:

javascript 复制代码
    {
        "code":"200",
	    "data":[
            {"id":111,"userName":"张三","userPassword":"123"},
	        {"id":222,"userName":"李四","userPassword":"123"},
	        {"id":333,"userName":"王五","userPassword":"123"},
        ],
	    "msg":"登陆成功",
    }

然后呢,我们就可以遍历这个result对象。

result.data : 表示你想要遍历的对象是什么。

index 是下标。

obj 就代表你想要遍历的每一个元素。

javascript 复制代码
<script>
    $.ajax({
        url: "/LoginServlet",
        data: {"name": name, "pwd": password},
        dataType: "json",
        type: "post",
        success: function (result) {
            if (result.code == "200") {
                alert(result.msg);
            }
            $.each(result.data, function (index, obj) {
                $("#111").html(obj.id);
                $("#222").html(obj.userName);
                $("#333").html(obj.userPassword);
            })
        }
    })
</script>
相关推荐
没差c1 天前
RetrofitClient写接口、导包、配置、路径
java·spring boot·okhttp
wcy03103 天前
POST为什么会发送两次请求
javascript·ajax
刺客码5 天前
Layui 表格固定列行高错位问题解决方案
前端框架·layui·jquery·web
事圆则缓12 天前
OkHttp + Retrofit 协作原理
okhttp·retrofit
降临-max14 天前
接口自动化(TestNG+OKHttp)
软件测试·okhttp·自动化·testng
小刘在重生~14 天前
Django|Ajax 入门、JSON 返回、ECharts 图表、文件上传完整笔记
ajax·django·json
吴声子夜歌15 天前
Java扩展——OkHttp
java·开发语言·okhttp
এ慕ོ冬℘゜15 天前
前端实战:基于jQuery递归实现通用树形组织菜单(可直接复用)
前端·javascript·jquery
零域码客15 天前
一文掌握 jQuery + Flask 全栈核心知识点(前端交互 + 后端接口 + 前后端联调)
前端·python·ajax·flask·jquery·前后端联调·web全栈
暖焰核心15 天前
C++模板进阶——特化全解
javascript·c++·jquery