【问题标题】:how to display the php array in json format in ajax php codeigniter如何在ajax php codeigniter中以json格式显示php数组
【发布时间】:2016-05-26 08:00:54
【问题描述】:

我有几个错误

$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");
            },
        });
             
             

         });     
 });
    

我想在 ajax 中显示错误,我有上面的代码,如何使用 json 在 ajax 中显示错误。任何帮助将不胜感激。

【问题讨论】:

  • var obj = response;
  • @splash 这是什么意思
  • 我想在 else 部分显示错误
  • php中为什么不用&lt;br&gt;加入数组?
  • 我得到了答案,它显示在下面

标签: php jquery arrays json codeigniter


【解决方案1】:

var items=[];
$.each(obj, function(key, value) {
items.push(value);
});
由于它是一个关联数组,它将显示所有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多