2015-05-28 27 views
0

我使用Ajax上傳圖片而不刷新。但由於某些原因,如文件太大的錯誤信息沒有顯示。如果一切順利,沒有任何錯誤,它一切正常,我收到一條消息說它工作。我嘗試了以下2種方式:ajax上傳圖片不刷新,如果出現錯誤如何取回錯誤

form_data.append('file', file_data); 
$.ajax({ 
      url: 'upload.php', 
      dataType: 'json', 
      cache: false, 
      contentType: false, 
      processData: false, 
      data: form_data,       
      type: 'post', 
      success: function(response){ 
       if (response.errors == 0){ 
        //do something to show it went well 
       } 
       else{ 
        //do something to show it went wrong 
       } 
      } 

那麼一些,而之後我意識到,上述可能是錯誤的,因爲它僅作用如果上傳是更迭。所以,我想這個代替:

  success: function(response){ 
       //do something to show it went well 
      }, 
      error: function(response){ 
       //do something to show it went wrong 
       } 

但是,這並不工作,要麼

是上傳圖片的腳本是這樣的:

if ($_FILES["file"]["size"] > 5000000) { 
     echo json_encode(array('errors'=>1,'naam_orgineel'=>$_FILES['file']['name'], 'errormsg'=>'file size is to big')); 
     $$errors = 1; 
    } 

    if ($errors == 0){ 
    move_uploaded_file($_FILES['file']['tmp_name'], $target); 

    echo json_encode(array('errors'=>0,'naam_orgineel'=>$_FILES['file']  ['name'], 'naam'=>$target_file)); 
    } 

有誰知道爲什麼錯誤消息不以兩種方式顯示?

+0

你有$$錯誤和$錯誤。由於未設置$ error,因此您從不設置錯誤消息 – mplungjan

回答

0

嘗試用響應失敗

var request = $.ajax({ 
    ...... 
}); 

request.done(function(msg) { 
    .... 
}); 

request.fail(function(jqXHR, textStatus) { 
    alert("Request failed: " + textStatus); 
});