2016-11-12 18 views
5

に不明にmultipart/form-dataのPOSTデータ内の境界がありませんこれは私のjavascriptのPHPのメッセージ警告:行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> 

このコードのアイデアは次のとおりです。 FirstNameとImageのような基本的な情報を、ページをリフレッシュせずにデータベースのライブに挿入したい。なぜ私はajaxを提出するタグを選択する理由。 Account.php?Edit = 1のページURLは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-データ未知数 - ライン0) –

答えて

4

手動で何も指定していない場合、ブラウザーは正しいヘッダー(Content-typeに正しい複数区画境界を含む)を設定します。それはUploading a file with XMLHttprequest - Missing boundary in multipart/form-dataにElliasヴァンOotegemによって提案されたよう

hr.setRequestHeader("Content-type", "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 –