在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>
相关推荐
CYY956 天前
OkHttp 和 Retrofit 封装使用
okhttp·retrofit
CYY957 天前
OkHttp 的使用
okhttp
学以智用18 天前
jQuery DataTables 完整实用教程
jquery
朝星18 天前
Android开发[14]:网络优化之OkHttp
android·okhttp·kotlin
学以智用20 天前
jQuery `serialize()` 详解
jquery
এ慕ོ冬℘゜20 天前
jQuery 高可用多图上传组件(企业级封装 + 踩坑全解 + 可直接上线)
前端·javascript·jquery
之歆20 天前
Promise 基础技术深度解析:从回调地狱到链式调用
前端·okhttp·promise
wuxia211820 天前
在5种环境中编写点击元素改变内容和颜色的JavaScript程序
javascript·微信小程序·vue·jquery·react
之歆20 天前
Ajax 基础技术深度解析:XHR 从入门到跨域
前端·ajax·okhttp