0

I've given up searching for answers after 4 hours today. I hope someone is able to see something I've done that is just stupid.

I'm using a simple XMLHttpRequest to pass formdata. Appending key and value via for loop.

     let _data = new FormData();
       for (var key in req) {
           var value = req[key];
             _data.append(key, value);
       }

Creating request, open and send

 let xhr = new XMLHttpRequest();
 xhr.open("POST", qb_url, true); 
 xhr.send(_data);

But the formdata is not appearing as valid key values when it hit the PHP page. I appear to get a array with one string encompassing everything, without the parsed key/value when I var_dump $_POST in PHP:

array(1) {
  ["------WebKitFormBoundaryI9nfSkkAwYNXDddt
Content-Disposition:_form-data;_name"]=>
  string(173) ""i"

6
------WebKitFormBoundaryI9nfSkkAwYNXDddt
Content-Disposition: form-data; name="comprobante"

5.060308220031026e 49
------WebKitFormBoundaryI9nfSkkAwYNXDddt--
"
}

Similar/strange output in debugger in Chrome: https://drive.google.com/file/d/12IEDmctiYChcoeGgZjVPluZaGHotOu_t/view?usp=sharing

  • Try `xhr.setRequestHeader("Content-type", "multipart/form-data");` – Barmar Aug 30 '22 at 20:42
  • Thanks and sorry, I left out the header for brevity, but the tried your suggestion with similar results: Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0
    array(0) { } I am using xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
    – Dave Playfair Aug 30 '22 at 20:59
  • You shouldn't be using `x-www-form-urlencoded`, you should be using `multipart/form-data`. That's the problem. – Barmar Aug 30 '22 at 21:01
  • Thanks, but I neither work and I don't think I need to use multipart/form-data for this simply call. See https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data – Dave Playfair Aug 30 '22 at 21:04
  • 1
    That's true, you shouldn't need to. When you look at the request headers in the console, what does it show for Content-type? – Barmar Aug 30 '22 at 21:10
  • Hi Barmar, this is what I see in total (taking out a few lines): POST /GetComprobante.php HTTP/1.1 ... Connection: keep-alive Content-Length: 281 Cookie: PHPSESSID=5ohgeoi5s03e71bm66hi5j25el Host: localhost Origin: http://localhost Referer: http://localhost/mh_comprobantes.html Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 content-type: application/x-www-form-urlencoded sec-ch-ua: "Chromium";v="104", " Not A;Brand";v="99", "Google – Dave Playfair Aug 30 '22 at 22:20
  • Thanks Barmar, here is what it shows: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 content-type: application/x-www-form-urlencoded sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105" – Dave Playfair Aug 30 '22 at 22:36
  • Notice that it still says `application/x-www-form-urlencoded`. Something you're doing is overriding the default. Please post all the code you use to send the AJAX request. Or switch to `fetch()` -- there's no excuse for using XMLHttpRequest in 2022. – Barmar Aug 30 '22 at 22:48
  • Please include a minimal reproducible example; what's `req`? You're using scoped variables, but it's hard to tell where their scope extends to because you've separated chunks of code. – mykaf Aug 31 '22 at 14:47
  • Getting back to this, I was never able to resolve why it behaved this way, so I took Barmar's advice and just used Fetch. No problems there so gave up on digging into this. I did consider the scope of the variables and that did not seem to be the issue. – Dave Playfair Oct 13 '22 at 15:58

0 Answers0