2015-01-31 3 views
-1

ajax를 사용하여 이미지를 서버에 업로드하려고하는데 AJAX에서 데이터를 반환하면 문제가 respondText로 정의되지 않습니다.Jquery AJAX responseText undefined

$('#imageform').submit(function(e){ 

var formData = new FormData(this); 
    var request = $.ajax({ 
    url: "<? echo site_url('mybazaar/uploadProductImage'); ?>", 
    type: "POST", 
    data: formData, 
    contentType: false, 
    processData: false, 
    success: function (res) { 
     var newImage = $(res).filter('‪#ajaximage').html(); 
     $('#ajax_product_image').append(newImage); 
     alert(res.responseText); 
    }, 
    error: function (request, status, error){ 
     alert(error); 
    } 
    }).done(function(){ 

    }); 
e.preventDefault(); 
}); 

PHP 파일 : 그러나

public function uploadProductImage(){ 
    $config['upload_path']   = './images/product_images/'; 
    $config['allowed_types']  = 'gif|jpg|png'; 
    $config['max_size']    = 10024; 
    $config['max_width']   = 10240; 
    $config['max_height']   = 10240; 

    $upload_path = 'images/product_images/'; 

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

    if (!$this->upload->do_upload("product_image_primary")){ 
     return false; 
    } 
    else{ 
     $data = array('upload_data' => $this->upload->data()); 
     $this->resize($data['upload_data']['full_path']); 
     echo "<div id='ajaximage'><div class='col-md-2'><img src='".base_url().$upload_path.$data['upload_data']['file_name']."'></div></div>"; 
    } 
} 

경우 내가에만 할 "경고 (고해상도)", 전체 페이지의 HTML 내가 "mybazaar/uploadProductImage"에 반향 텍스트를 포함한 경고한다.

나는 Jquery 1.9와 CodeIgniter를 사용하고 있습니다.

+0

더 나은 도움을 받으려면 PHP 파일을 보여주세요. – Sarath

+0

'res.responseText'에 무엇을 포함 할 것으로 기대하십니까? – JJJ

+0

PHP 파일로 편집 된 res.responseText는 PHP 파일에 에코 된 데이터를 포함해야합니다. –

답변

0

success 콜백의 첫 번째 인수는 content-type 헤더에 지정된 형식으로 서버에서 반환 된 실제 데이터입니다.이 데이터는 기본적으로 html이며 귀하의 경우에도 마찬가지입니다. 이제 html로 돌아가므로 filter('‪#ajaximage') 을 사용하여 div를 검색 할 필요가 없습니다. 정확히 div # ajaximage를 가지고 있기 때문에 res에있는 div를 직접 추가 할 수 있으므로 DOM에 직접 추가 할 수 있습니다. responseText 성공 콜백의 데이터 인수에 정의 된 속성이 아니기 때문에

success: function (res) { 
     $('#ajax_product_image').append(res); 
    } 

내가 alert(res.responseText)을 제거, 그것은 Ajax 호출을 발사하는 데 사용되는 실제 XMLHttpRequest 객체의 속성입니다. responseText에 대한 액세스가 여전히 필요하다면 성공 콜백은 3 개의 인수를가집니다. function(data, status, jqXHR). jqXHR(3rd argument basically, you can change the name if you like) will have all the underlying properties of original XMLHttpRequest` 객체

+0

안녕하세요, 여러 div가 있고 페이지의 다른 위치에 추가하고 싶습니다. 전에 다른 프로젝트에서 $ (res) .filter ('# idtobefiltered') 코드를 사용했습니다 .html()하지만 codeigniter에서는 작동하지 않습니다. –

+0

그런 다음 다른 div에 동봉하십시오. 이제 res가 외부 div가되고 필터가 평소와 같이 작동합니다. – Arkantos

+0

"ajaximage"div로 묶여 있습니다. 필터링하는 경우 정의되지 않습니다. –