2016-11-12 3208 views
5

這是我的javascriptPHP消息警告:缺少的multipart/form-data的POST數據邊界在未知在線0

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> 

這段代碼的想法是。我想插入像名字和圖像的基本信息到數據庫中而不刷新頁面。我之所以選擇標籤來提交ajax。所以頁面URL Account.php?Edit = 1不會更改爲Account.php,因爲如果它更改爲account.php,則彈出式編輯模式將關閉。這個代碼中的問題是。我不知道如何解決這個錯誤Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0任何人都可以幫助我解決這個問題。謝謝!

+0

可能重複的[警告:缺少多行/表單數據POST數據在行0中未知的邊界](http://stackoverflow.com/questions/27635738/warning-missing-boundary-in-multipart-form-data-post- data-in-unknown-on-line-0) –

回答

4

如果您尚未手動指定任何內容,瀏覽器會設置正確的標題(包括Content-type中的正確的多部分邊界指示)。

因此,所有你需要做的就是刪除以下行:

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

否則,你需要自己設定的邊界,它是由Ellias範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)); 
+0

根據Elias Van Ooteg的評論em:http://stackoverflow.com/questions/12348216/uploading-a-file-with-xmlhttprequest-missing-boundary-in-multipart-form-data –

相關問題