【发布时间】:2019-11-13 22:13:55
【问题描述】:
我有一些像这样的 json 数据
{"errors":{"toDate":["Date error_message"],"name":["name error_message"]}}
OR
{"success":{"toDate":["Date update_data"],"name":["name update_data"]}}
如何使用 jQuery、javascript 循环遍历它!
通过ajax请求从服务器返回json数据!
所以如果 json {"errors":....}
我需要显示错误如果响应成功则更新表行 if 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 的解决方案,但出现以下错误:
未捕获的类型错误:无法读取未定义的属性“错误”
已解决::
我忘记在 ajax 函数中设置 dataType 了!!! 数据类型:json
【问题讨论】:
-
这些数组总是只有一个元素吗?
-
@Barmar 是的,只有一个元素
标签: javascript jquery json