【问题标题】:Reading response text in Ajax using JQuery使用 JQuery 在 Ajax 中读取响应文本
【发布时间】:2014-08-06 16:00:24
【问题描述】:
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 作为输出。

【问题讨论】:

  • alert(textstatus.getResponseHeader('abc')) 为空是你的问题吗?
  • 是的,我回显了数据,但它仍然为空。
  • 你为什么使用textstatus.getResponseHeader()而不是仅仅访问应该包含响应正文的data参数?
  • @MattBrowne 我需要回复文本。
  • 太棒了。仅供参考,textstatus.getResponseHeader() 不是获取响应文本的方式 - 它仅返回响应的标头,这通常不是您想要的。

标签: javascript php jquery ajax codeigniter


【解决方案1】:

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

例子:

$.ajax({


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

检查此link。更清楚地了解$.ajax() 将很有用。

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2010-12-10
    相关资源
    最近更新 更多