【问题标题】:php message Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0php 消息警告:在第 0 行未知的多部分/表单数据 POST 数据中缺少边界
【发布时间】:2017-03-26 11:54:32
【问题描述】:

这是我的javascript

function ajax_post(){
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create some variables we need to send to our PHP file
            var url = "LiveUpdate.php";
            var sb = document.getElementById("LiveUpdate").value;
            var FirstName = document.getElementById("FirstName").value;
            var images = document.getElementById("images").value;

            var vars = "update="+sb+"&FirstName="+FirstName+"&images="+images;
            hr.open("POST", url, true);
            // Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "multipart/form-data");
            // Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if(hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    document.getElementById("message").innerHTML = return_data;
                }
            }
            // Send the data to PHP now... and wait for response to update the status div
            var formData = new FormData(document.querySelector("form"));
            hr.send(formData); // Actually execute the request // Actually execute the request
            document.getElementById("message").innerHTML = "processing...";
        }
            //show image before uploading
                var loadFile = function(event) {
                var output = document.getElementById('output');
                output.src = URL.createObjectURL(event.target.files[0]);
                };//end image

这就是表格。 javascript 和表单在同一个文件中。文件名为sample.php

<form action="popup.php" method="post" id="myForm" name="myForm" enctype="multipart/form-data">
    <div id="message"></div>
    <img src="" id="output" />
    <input type="file" class="filestyle" data-buttonBefore="true" accept=".png, .jpg, .jpeg" onchange="loadFile(event)" name="images" id="images">
    <input class="form-control" type="text" placeholder="First Name" name="FirstName" id="FirstName" value="">
    <a href="#" class="btn btn-success" role="button" name="update" id="LiveUpdate"  onclick="javascript:ajax_post();">Update</a>
</form>

这段代码的想法是。我想在不刷新页面的情况下将 FirstName 和 Image 等基本信息实时插入数据库。我选择标签提交ajax的原因。所以页面 url Account.php?Edit=1 不会更改为 Account.php 因为如果更改为 account.php 弹出编辑模式将关闭。这段代码的问题是。我不知道如何解决错误Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0 任何人都可以帮我解决这个问题。谢谢!

【问题讨论】:

标签: javascript php


【解决方案1】:

如果您没有手动指定任何内容,浏览器会设置正确的标题(包括 Content-type 中正确的多部分边界指示)。

所以您需要做的就是删除以下行:

 hr.setRequestHeader("Content-type", "multipart/form-data");

否则您需要按照 Ellias Van Ootegem 在Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data 中的建议自行设置边界:

hr.setRequestHeader("Content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));

【讨论】:

  • 根据 Elias Van Ootegem 的评论:stackoverflow.com/questions/12348216/…
  • 伙计,我花了一整天的时间试图发送表单数据。那成功了。非常感谢!
  • boundary 添加到Content-Type 标头也解决了我的问题。
【解决方案2】:

这也有效。取自https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type

常量配置 = { 标题:{ 授权:Bearer ${localStorage.token}, “内容类型”: “多部分/表单数据;边界=---------------974767299852498929531610575” } };

【讨论】:

    【解决方案3】:

    只需删除 setRequestHeader()

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2015-02-22
    • 2016-05-01
    • 2014-10-31
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2014-05-07
    • 2016-01-15
    • 2021-10-24
    相关资源
    最近更新 更多