我正在尝试使用 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>
最佳答案
您需要为您的 html 表单分配 enctype="multipart/form-data"
属性。
关于javascript - 使用 jquery 上传文件 : POST 500 (Internal Server Error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40933203/