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

时间:2014-08-06 16:00:24

标签: javascript php jquery ajax codeigniter

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 Framework .Below是我处理文件的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"];
        }

     }

我尝试了多种风格来获取Response文本,但没有成功。完成后阅读响应文本应该是什么。 获得null作为输出。

1 个答案:

答案 0 :(得分:1)

$.ajax()的{​​{1}}方法中,第一个参数会给success

示例:

response

选中 link 。更清楚地理解$.ajax({ success: function(response, textStatus, jqXHR ){ console.log(response); alert(response.getResponseHeader('abc')); }, }); 会很有用。