在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>
相关推荐
渴望成为python大神的前端小菜鸟3 小时前
浏览器及其他 面试题
前端·javascript·ajax·面试题·浏览器
chilavert31812 小时前
技术演进中的开发沉思-235 Ajax:动态数据(上)
javascript·ajax·okhttp
灰什么鱼1 天前
OkHttp + Retrofit2 调用第三方接口完整教程(以nomad为例)
java·spring boot·okhttp·retrofit
苏打水com3 天前
第六篇:Day16-18 AJAX进阶+接口对接——实现“前后端数据交互”(对标职场“接口开发”核心需求)
css·okhttp·html·js
漏洞文库-Web安全3 天前
CTFHub XSS通关:XSS-过滤关键词 - 教程
前端·安全·web安全·okhttp·网络安全·ctf·xss
chilavert3183 天前
技术演进中的开发沉思-228 Ajax: Aptana开发
前端·javascript·ajax
士心凡3 天前
Spark
大数据·ajax·spark
chilavert3183 天前
技术演进中的开发沉思-229 Ajax:Firefox 与 Firebug
javascript·okhttp
码界奇点4 天前
Java Web学习 第15篇jQuery从入门到精通的万字深度解析
java·前端·学习·jquery
beijingliushao4 天前
100-Spark Local模式部署
大数据·python·ajax·spark