在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>
相关推荐
liangshanbo12156 小时前
面试题:页面需要发起大量 Ajax / HTTP 请求时,如何优化请求性能?
前端·http·ajax
RobinDevNotes8 小时前
JAX 分布式训练,和 PyTorch 有什么不一样
人工智能·深度学习·ajax
小七在进步8 小时前
类和对象(四)
java·javascript·ajax
没差c9 天前
RetrofitClient写接口、导包、配置、路径
java·spring boot·okhttp
wcy031011 天前
POST为什么会发送两次请求
javascript·ajax
刺客码13 天前
Layui 表格固定列行高错位问题解决方案
前端框架·layui·jquery·web
事圆则缓20 天前
OkHttp + Retrofit 协作原理
okhttp·retrofit
降临-max22 天前
接口自动化(TestNG+OKHttp)
软件测试·okhttp·自动化·testng
小刘在重生~22 天前
Django|Ajax 入门、JSON 返回、ECharts 图表、文件上传完整笔记
ajax·django·json
吴声子夜歌23 天前
Java扩展——OkHttp
java·开发语言·okhttp