在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>
相关推荐
Zheng1137 小时前
【可视化大屏】将柱状图引入到html页面中
javascript·ajax·html
前端李易安8 小时前
ajax的原理,使用场景以及如何实现
前端·ajax·okhttp
萧鼎19 小时前
Python常见问题解答:从基础到进阶
开发语言·python·ajax
王小二(海阔天空)20 小时前
个人文章合集 - 前端相关
前端·css·vue·jquery
儒雅的烤地瓜1 天前
JS | 如何解决ajax无法后退的问题?
前端·javascript·ajax·pushstate·popstate事件·replacestate
学习使我快乐011 天前
AJAX 1——axios体验、认识URL、常用请求方法、HTTP协议、错误处理、form-serialize插件
前端·http·ajax·okhttp·axios
帅次2 天前
解决 Android WebView 无法加载 H5 页面常见问题的实用指南
android·okhttp·gradle·binder·webview·retrofit·appcompat
冬至喵喵3 天前
SPARK调优:AQE特性(含脑图总结)
大数据·ajax·spark
懒洋洋大魔王4 天前
7.Javaweb-Ajax
前端·ajax·okhttp
人生の三重奏4 天前
前端——Ajax和jQuery
前端·ajax·jquery