簡體   English   中英

使用jquery上傳文件:POST 500(內部服務器錯誤)

[英]file upload using jquery: POST 500 (Internal Server Error)

我正在嘗試使用jQuery和POST方法將圖像和一些輸入上傳到服務器。 我嘗試了這段代碼,但它使我出錯: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> 

與該飼料我有這個輸出: 在此處輸入圖片說明

調試在此部分停止時,似乎是客戶端發出了問題,因為在servur中需要映像,該映像不必為null,因此這是他引發錯誤的原因。 在此處輸入圖片說明

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

這個正確的代碼,只需要更改服務器的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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM