首页 > 解决方案 > Ext.JSON.decode(): You're trying to decode an invalid JSON String 访问/source/index.php/file/upload/时出现问题

问题描述

提交我的 ExtJS 表单后出现以下错误

安慰:

Ext.JSON.decode(): You're trying to decode an invalid JSON String 访问/source/index.php/file/upload/时出现问题

JS 控制器文件:

    uploadFile: function (button, e, eOpts) {
    var form = button.up('form');

    if(form.isValid()){
        form.submit({
            url: 'source/index.php/file/upload/',
            waitMsg: 'Upload Gambar',
            success: function(fp, o) {
                if(o.result.success == 'true'){
                    Ext.Msg.alert('Sukses', 'Gambar Anda "' + o.result.file + '" Telah Diunggah.'); 
                }else{
                    Ext.Msg.alert('Error', 'Ada Kesalah, Mohon Di Cek Kembali');
                }                  

                //console.log(o.result);
            }
        });
    }
}

PHP Codeigniter 文件有:

public function upload(){
    $answer = array("success"=>"false");


    $flag = true;

    $error=array();
    $info=array();
    $config=array();

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload("imagen")) //falta modificar la foto para que encaje con el numero de dni
    {
        $error = array('error' => $this->upload->display_errors());         
    }
    else
    {           
        $info=$this->upload->data();
        $name= $info['file_name'];
        $tmp= $info['full_path'];   
        $sql= "insert into  file (id,name,data) values (NULL,'".$name."', load_file('".$tmp."') );";

        $answer = array("success"=>"true","file"=>$name);

        $this->model_file->execute($sql);   

    }


    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($answer);      
}

存储 JS:

proxy: {
        type:'ajax',
        api:{
            create:     'source/index.php/file/create',
            read:       'source/index.php/file/all',
            update:     'source/index.php/file/update',
            destroy:    'source/index.php/file/delete'
        },
        reader:{
            type: 'json',
            rootProperty: 'data'
        },
        writer: {
            type:'json',
            rootProperty: 'data',
            writeAllFields: true,
            encode: true,
            allowSingle: true
        }
    },

解决办法是什么?我正在使用 Ext JS 6 和 Codeigniter 框架

标签: phpjson

解决方案


推荐阅读