Search code examples
javascriptpostxmlhttprequest

Cannot access $_POST or $_REQUEST or 'php://input' - all are NOT empty


Trying to make a simple xhr request with a few variables and a long JSON. When I var_dump($_POST), var_dump($_REQUEST), or var_dump(file_get_contents('php://input')), I get this:

array(1) {
  ["------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition:_form-data;_name"]=>
  string(5725) ""document_id"

608
------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition: form-data; name="project_id"

2
------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition: form-data; name="user_id"

42
------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition: form-data; name="stage"

1
------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition: form-data; name="show_save_button"

1
------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition: form-data; name="id_user_associatedDoc"

42
------WebKitFormBoundarySaVZDjpQjjwsAOHs
Content-Disposition: form-data; name="annotation"

{"nodes":[{"id":0,"x":67,"y":101,"text":"ng heard. Engaged at village at am equally proceed. Settle nay length almost ham direct extent. Agreement for listening remainder get attention law acuteness","width":229,"height":100,"type":"I","color":"b","scheme":0,"visible":true},{"id":1,"x":325,"y":59,"text":"Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. Entire any had depend and figure winter. Change stairs and men likely wisdom new happen piqued six. Now taken him timed sex world get. Enjoyed married an feeling delight pursuit as offered. As admire roused length likely played pretty to no. Means had joy miles her merry solid order. ","width":200,"height":2420,"type":"I","color":"b","scheme":0,"visible":true}],"edges":[],"metadata":{"documentId":"608","projectId":"2","ranges":[[554,760],[17,302]],"idUserAssociatedDoc":"42"}}
------WebKitFormBoundarySaVZDjpQjjwsAOHs--
"
}

But once I try to access the fields, I get NULL :(

I've tried different Content-Types (multipart/form-data , application/x-www-form-urlencoded, or none)

The xhr:

    var xhr = new XMLHttpRequest();
    xhr.open('POST', resultsURL, true);
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.onload = function () {
        // do something to response
        console.log(this.responseText);
    };

    var dataSave = new FormData();

    dataSave.append('document_id', documentId);
    dataSave.append('project_id', projectId);
    dataSave.append('user_id', userId);
    dataSave.append('stage', stage);
    dataSave.append('show_save_button', showSaveButton);
    dataSave.append('id_user_associatedDoc', idUserAssociatedDoc);
    dataSave.append('annotation', JSON.stringify(json));

    xhr.onreadystatechange = function() {
        switch(xhr.readyState) {
            case XMLHttpRequest.OPENED:
            case XMLHttpRequest.LOADING:
                if(callbackLoading)
                    callbackLoading();
                break;
            case XMLHttpRequest.DONE:

                    console.log("XMLHttpReques.responseText:");
                    console.log(xhr.responseText);


                break;
        }
    }

    xhr.send(dataSave);

On the resultsURL php:

    $docId = $_REQUEST['document_id'];
    echo var_dump($docId); //returns NULL

Isn't this supposed to work? Do you know what I might be doing wrong?


Solution

  • multipart/form-data

    new FromData is multipart/form-data with a boundary (https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html). you should not set any content-type header. The Header will be set by the browser. you can access the data with $_POST or $_REQUEST.

    application/x-www-form-urlencoded

    if you use application/x-www-form-urlencoded you have to send the date url encoded.

    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    // ...
    
    function encodeForm(dataSave) {
      bar r = [];
      dataSave.forEach((v,k) => {
        r.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
      });
      return r.join('&');
    }
    xhr.send(encodeForm(dataSave));
    

    you can access the data with $_POST or $_REQUEST

    application/x-www-form-urlencoded has less overhead.