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


Reading response text in Ajax using JQuery

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');
         }  
    }
}

我使用CodeIgniter框架。下面是我的PHP代码来处理文件。

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"];
        }
     }

我尝试了多种方式来获得响应文本,但没有成功。完成后应该读什么回应文本。得到null作为输出。

$.ajax()success方法中,第一个参数将给出response

的例子:

$.ajax({

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

查看链接。这将有助于更清楚地理解$.ajax()