我有以下格式的json响应:
[ { "0": [ { "tVote": "32" } ], "1": [ { "choice": "Barcelona", "tbAns": "2" }, { "choice": "Bayern Muenchen", "tbAns": "2" }, { "choice": "Juventus", "tbAns": "20" }, { "choice": "Manchester United", "tbAns": "5" }, { "choice": "Real Madrid", "tbAns": "3" } ], "2": [ { "question": "Favorite football team ?" } ], "status": "positive", "msg": "Thank you, your vote has been count." } ]
到目前为止,我在以下代码中使用jQuery ajax访问它:
$(function() { $('.vote').click( function(e) { e.preventDefault(); var parId = $(this).closest('form').attr('id'); var itemId = $(this).prev('input[name=q_id]').val(); var formAction = $('#' + parId).attr('action'); $.ajax({ type : 'POST', url : formAction, data : $('#' + parId + '').serializeArray(), dataType : 'json', beforeSend: function() { $('.loadPoll-' + itemId).removeClass('hidden'); }, error : function(request, status, error) { $('#' + parId).html('sorry can't send your request, please try again later
' + status + ' ' + error); }, success : function(data) { $('.loadPoll-' + itemId).addClass('hidden'); $.each(data, function() { var theQ = data[0][2][0]['question']; var msg = data[0]['msg']; var status = data[0]['status']; var totalVoter = data[0][0][0]['tVote']; var item = data[0][1]; var itemLength = data[0][1].length; var itemChoice = data[0][1][0].choice; var parental = $('.vote:focus').closest('form').attr('id'); //create html template for response var template = ''; if(status === 'negative') { $.amaran({ content : { bgcolor: '#FF9900', message: msg, color : '#fff', icon : 'fa fa-download' }, theme : 'colorful', position : 'bottom right', cssanimationIn : 'swing', cssanimationOut: 'bounceOut' }); template += '' + theQ + '
Total voter: ' + totalVoter + '
以上就是jQuery教程分享访问json对象的最佳或正确方法?相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/558986.html
赞 (0)
jQuery技术:img.on(“load”)在加载img之后但在它的大小设置(IE)之前执行
上一篇
2021年2月1日
jQuery技术:发布function错误:找到没有类型名称的条目
下一篇
2021年2月1日