gpt4 book ai didi

javascript - 需要循环遍历特定 json 数据格式的帮助

转载 作者:行者123 更新时间:2023-12-02 17:52:54 24 4
gpt4 key购买 nike

我有一些如下所示的 json 数据

{"errors":{"toDate":["Date error_message"],"name":["name error_message"]}}

OR

{"success":{"toDate":["Date update_data"],"name":["name update_data"]}}

如何使用 jQuery、javascript 循环遍历此内容!

json数据是通过ajax请求从服务器返回的!

所以我需要显示错误 if json {"errors":....}

或者如果 json {"success":...} 则响应成功则更新表行

这是我的ajax调用函数

jQuery('#juiDialog').on('submit','#formn-update',function(e){
e.preventDefault();
e.stopPropagation();
jQuery.ajax({
Type:'json',
global : false,
async : false,
cache : false,
type : 'POST',
url : jQuery(this).attr('action'),
data:jQuery(this).serialize()
}).success(function(data) {
alert(data.errors.toDate[0]);
});
});

我尝试了@Barmar 的解决方案,但出现以下错误:

Uncaught TypeError: Cannot read property 'errors' of undefined

已解决::

我忘了在ajax函数中设置dataType!!!数据类型:json

最佳答案

jQuery.ajax({
global: false,
async: false,
cache: false,
type: 'POST',
dataType: 'json', // Tell jQuery to parse the JSON result
url: jQuery(this).attr('action'),
data: jQuery(this).serialize()
}).success(function (data) {
if (data.errors) {
alert(data.errors.toDate[0]);
} else {
// do something with data.success.toDate[0] and data.success.name[0]
}
});

关于javascript - 需要循环遍历特定 json 数据格式的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21203649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com