2016-12-02 115 views
1

我試圖上傳圖像和一些輸入到服務器,使用Jquery,POST方法。我試過這個代碼但它是拋出我的錯誤: POST 500(內部服務器錯誤)。 有人能幫我弄清楚代碼有什麼問題。感謝您的幫助。使用jquery上傳文件:POST 500(內部服務器錯誤)

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Image Upload Form</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <script type="text/javascript"> 
 
     function submitForm() { 
 
      console.log("submit event"); 
 
      var fd = new FormData(document.getElementById("fileinfo")); 
 
      fd.append("label", "WEBUPLOAD"); 
 
      $.ajax({ 
 
       url: "http://URL?api_token=fb24085da58dad6decb9271fb170ef2ed8c80617", 
 
       type: "POST", 
 
       data: fd, 
 
       processData: false, // tell jQuery not to process the data 
 
       contentType: false // tell jQuery not to set contentType 
 
      }).done(function(data) { 
 
       console.log("PHP Output:"); 
 
       console.log(data); 
 
      }); 
 
      return false; 
 
     } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();"> 
 
     <label>Select a file:</label><br> 
 
     <input type="file" name="file" required /> 
 
     <input type="text" name="text" required /> 
 
     <input type="submit" value="Upload" /> 
 
    </form> 
 
    <div id="output"></div> 
 
</body> 
 
</html>

隨着Fidder酒店,我有這樣的輸出:enter image description here

當調試運行停止在這一部分,似乎這個問題是從客戶正在添加,因爲在serveur圖像是必需的,它不能爲空,所以這就是他拋出錯誤的原因。 : enter image description here

+0

是您的URL真正的 「http:// URL api_token = fb24085da58dad6decb9271fb170ef2ed8c80617」 還是你替換 'URL' 真實的URL,當你張貼的問題嗎? –

+0

是的,我確實取代了它(我沒有權利發佈它) – Amina

+1

您的表單應該有enctype =「multipart/form-data」。見[這個問題](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean)。 –

回答

2

您需要將enctype="multipart/form-data"屬性分配給您的html表單。

+0

謝謝你的回覆。 form method =「post」id =「fileinfo」name =「fileinfo」onsubmit =「return submitForm();」 enctype =「multipart/form-data」>但仍然有錯誤,檢查後我發現這個錯誤,這意味着服務器沒有收到圖像。你認爲這個問題是什麼。 SQLSTATE [23000]:完整性約束違規:1048列'img'不能爲null(SQL:插入到'menus'('img','user_id','restaurant_id','updated_at','created_at')values ,3,2016-12-02 13:59:50,2016-12-02 13:59:50)) – Amina

+0

您是否正確映射參數?我的意思是表單輸入字段和服務器端參數。提交請求後,輸入字段將作爲「文件」和「文本」提交。你在服務器端有相應的參數嗎? – user1080381

+0

來自服務器的SQL錯誤:Connection.php中的QueryException線761: SQLSTATE [23000]:完整性約束違規:1048列'img'不能爲空(SQL:插入'menus'('img','user_id ','restaurant_id','updated_at','created_at')values(,9,3,2016-12-02 14:37:18 2016-12-02 14:37:18))==>看來服務器沒有收到圖像 – Amina

2

這是正確的代碼,只需要更改服務器的URL。感謝user1080381,他在評論中給了我解決方案。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Image Upload Form</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <script type="text/javascript"> 
 
     function submitForm() { 
 
      console.log("submit event"); 
 
      var fd = new FormData(document.getElementById("fileinfo")); 
 
      console.log(fd); 
 
      //fd.append("label", "WEBUPLOAD"); 
 
      console.log(fd); 
 
      $.ajax({ 
 
       url: "http://TypeYourURL?api_token=fb24085da58dad6decb9271fb170ef2ed8c80617", 
 
       type: "POST", 
 
       data: fd, 
 

 
       processData: false, // tell jQuery not to process the data 
 
       contentType: false // tell jQuery not to set contentType 
 
      }).done(function(data) { 
 
       console.log("PHP Output:"); 
 
       console.log(data); 
 
      }); 
 
      return false; 
 
     } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();" enctype="multipart/form-data"> 
 
    <label>Select a file:</label><br> 
 
    <input type="file" name="img" required /> 
 
     <input type="text" name="name" required /> 
 
     <input type="submit" value="Upload" /> 
 
     </form> 
 

 

 
     
 
    <div id="output"></div> 
 
</body> 
 
</html>

相關問題