dousi9215 2014-08-06 16:00
浏览 48

使用JQuery在Ajax中读取响应文本

function upload_file(){
    var file =document.getElementById('computer_image').files[0];
    if(file!==null){
        if(file.type==='image/jpeg' ||
            file.type==='image/png' ||file.type==='image/jpg'){
            $('#progressbar').show();
                        var formData = new FormData();
                        formData.append("file1", file);
                       $.ajax({
                            url: 'file_upload/ImageUpload', 
                            type: 'POST',
                            headers: {"abc": "aaaaaaaaa"},
                            xhr: function() {
                                var myXhr = $.ajaxSettings.xhr();
                                if(myXhr.upload){
     myXhr.upload.addEventListener('progress',progressHandler, false); 
                                }
                                return myXhr;
                            },
                            success: function( request,data, textstatus){
        alert(textstatus.getResponseHeader('abc'));
   },
                            error:errorHandler,
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false
                        });
          }   
         else {
            alert('sorry we are not accepting file other than  PNG , JPEG and JPG');
         }  
    }
}

I am using CodeIgniter Framework .Below is my PHP code to Process a file .

function ImageUpload(){
    $status="";
    if (empty($_FILES["file1"]))
    {
        $status = "sorry Something went wrong";
        $this->output->set_status_header('400');
        echo "sorry Something went wrong";
    }
    if ($status !== "sorry Something went wrong")
    {

        //call a function to get path name
        $path="./upload";
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size' ] = '0';
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['remove_spaces']=TRUE;
        $config['overwrite']  = FALSE;
        $this->load->library('upload', $config);
        /* If directory doesn't exist than create a new .*/
        if (!file_exists($path) && !is_dir($path)) {
              mkdir($path);         
        } 
        /* If there is any error during upload  */
        if ( ! $this->upload->do_upload('file1')){
            $this->output->set_status_header('400');
            echo "sorry Something went wrong";
        }   
         /*Image has been successfully Uploaded */
         else{
           $var = $this->upload->data('','');
                echo $path.'/'.$var["file_name"].'='.$var["image_width"].
                 '='.$var["image_height"];
        }

     }

I have tried multiple flavor to get Response text but didn't success . What should be after complete to read response text . getting null as output .

  • 写回答

1条回答 默认 最新

  • dongyuan8024 2014-08-06 17:42
    关注

    In $.ajax()'s success method first parameter will give response

    Example:

    $.ajax({
    
    
        success: function(response, textStatus, jqXHR ){
            console.log(response);
            alert(response.getResponseHeader('abc'));
        },
    });
    

    Check this link. It will be useful to understand $.ajax() more clearly.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败