javascript - 使用 jquery 上传文件 : POST 500 (Internal Server Error)

标签 javascript jquery file-upload http-status-code-500

我正在尝试使用 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>

有了 fidder,我得到了这个输出:enter image description here

当调试在这部分停止时,问题似乎来自客户端,因为在服务器中需要图像,它不能为空,所以这就是他抛出错误的原因。 : enter image description here

最佳答案

您需要为您的 html 表单分配 enctype="multipart/form-data" 属性。

关于javascript - 使用 jquery 上传文件 : POST 500 (Internal Server Error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40933203/

相关文章:

javascript - 获取和javascript?就像 php 的 if $_GET[foo]

javascript - TS 2411 - 获取类型 'propertyName' 的错误属性 'string' 不可分配给字符串索引类型

Javascript-将数组中的值匹配到变量的子字符串

javascript - 用于客户端 Web 应用程序的开发/构建工具

javascript - 滚动时移动的按钮在单击时过早消失

javascript - 基于浏览器的本地文件系统到 SVG base64 数据字符串

asp.net-mvc - 使用 ASP.NET MVC 2 上传文件的最佳方式是什么?

javascript - 如何创建一个自动将内容自动换行的 div,以便 div 永远不会水平增长大于其容器?

javascript - 如何将我的数组对象中的数据获取到我的jstree?

javascript - 使用 CSS 样式和上传按钮的功能