dscc90150010 2016-05-26 08:00
浏览 46
已采纳

如何在ajax php codeigniter中以json格式显示php数组

I have couple of errors

$errors = array();
$errors['failed'] = "File uplaoded failed Please correct the following error";
$errors['size'] = 'File size must be 100 MB';
$errors['ext'] = "This formate does not support, Only .zip supported";
$errors['exits'] = "Sorry, file name already exists ,Please enter another theme name";
echo json_encode($errors)

$(function() {
     $('#addTheme').submit(function(e) {         
               e.preventDefault(); //ok
               var data = new FormData($(this)[0]);
              $.ajax({
                 url: '<?php echo base_url() ?>download/uploaded',
                 type: 'POST',
                 data: data,
                 dataType : 'json',
                 contentType: false,
                 cache: false,
                 processData: false,

                 success : function(response) {
                     $('.populate').val('');
                   var obj = jQuery.parseJSON(JSON.stringify(response));
                   if(obj.value === 1) {
                     $('#error').html('<div class="alert alert-success alert-dismissible" role="alert">'+
                        '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+'File uploaded successfully</div>');
                 } else {
                    $('#error').html('<div class="alert alert-success alert-dismissible" role="alert">'+
                        '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+'File uploaded failed'+   +'</div>');
                 }
                 console.log("upload success");
            },
        });
             
             

         });     
 });
    

i want to display the errors in ajax, i have the above code, how could i display the error in ajax using json. any help would be appreciated.

</div>
  • 写回答

1条回答 默认 最新

  • dtqi87613 2016-05-26 08:45
    关注

    var items=[];
    $.each(obj, function(key, value) {
    items.push(value);
    });

    since it is an associative array, it will display all the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序