2016-12-02 172 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>