dongyu1983 2019-05-11 07:20
浏览 300

获取formData后$ _POST为空

when I try to fetch a formData with POST from JS to PHP, I obtain an empty $_POST array(), despite of Status Code is 200 OK. Can you help me?

function select_collection()
 {
  const tendina=document.querySelectorAll(".tendina");
   for(all of tendina)
        {
           all.addEventListener('change', aggiungi);
        }
    }
    function aggiungi(event){
        const tendina=event.target;
        const val=tendina.options[tendina.selectedIndex].value; 

        const row_box= tendina.parentNode.parentNode; 
        const child=row_box.childNodes;

            const url_image=child[0].childNodes[0].currentSrc;

            const id_google=child[1].childNodes[0].textContent.slice(11,);
            const titolo= child[1].childNodes[1].textContent.slice(8,);
            const autore= child[1].childNodes[2].textContent.slice(8,);
            const editore=child[1].childNodes[3].textContent.slice(9,);
            const pagine=child[1].childNodes[4].textContent.slice(8,);
            var formData = new FormData();
            formData.append('titolo_racc', val);
            formData.append('id_google', id_google);
            formData.append('titolo', titolo);
            formData.append('autore', autore);
            formData.append('editore', editore);
            formData.append('pagine', pagine);
            fetch('http://localhost/progetto/inserisci_contenuto.php',{
            method: "POST",
            body: formData
          }).then(onResponse).then(onText);

.tendina it's the class of a drop-down menù (select, option), title, author, publisher and pages refers to a book volume infos. I want to send this info to a PHP page.

$conn=new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error)
{
    die ("Connection failed " .$conn->connect_error);
}

if(isset($_POST["titolo_racc"]) && isset($_POST["id_google"] && isset($_POST["titolo"] && isset($_POST["autore"] && isset($_POST["editore"] && isset($_POST["pagine"]))
{
    print_r($_POST); // array ()
    print_r($_POST["titolo"]; //Undefined index:
}
  • 写回答

2条回答 默认 最新

  • dpgu5074 2019-05-11 07:26
    关注

    Get rid of:

        headers: new Headers({
            "Content-Type": "application/json;"}),
    

    it's incorrect, since you're sending multipart/form-data format. You need to allow fetch() to provide this header by itself, since it needs to include the separator string, which is generated randomly internally.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大