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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?