在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>
相关推荐
必胜刻9 小时前
AJAX 请求理解
前端·ajax·okhttp·前后端交互
android_cai_niao10 小时前
OkHttp 使用教程:从入门到精通(Kotlin)
okhttp·kotlin
_周游2 天前
Kaptcha—Google验证码工具
java·intellij-idea·jquery
2601_948606183 天前
从 jQuery → V/R → Lit:前端架构的 15 年轮回
前端·架构·jquery
未完成的歌~3 天前
前端 AJAX 详解 + 动态页面爬虫实战思路
前端·爬虫·ajax
进击的雷神6 天前
AJAX动态参数反爬、HTML嵌套网站提取、UPSERT增量更新、空值智能处理——沙特塑料展爬虫四大技术难关攻克纪实
爬虫·python·ajax·html
qwert10377 天前
跨域问题解释及前后端解决方案(SpringBoot)
spring boot·后端·okhttp
符哥20089 天前
基于 OkHttp+Retrofit 实现 JSON / 表单 / XML/Protobuf 数据格式全解析
okhttp·json·retrofit
Cg1362691597410 天前
js引入方式
前端·javascript·ajax
Pu_Nine_910 天前
企业级 Axios 配置实战:从基础到完整封装
前端·ajax·axios·网络请求·企业级