Невозможно получить доступ к $ _POST или $ _REQUEST или 'php: // input' - все они не пусты - PullRequest
0 голосов
/ 07 апреля 2019

Попытка сделать простой запрос xhr с несколькими переменными и длинным JSON.Когда я var_dump($_POST), var_dump($_REQUEST) или var_dump(file_get_contents('php://input')), я получаю это:

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--
"
}

Но как только я пытаюсь получить доступ к полям, я получаю NULL: (

I 'мы пробовали разные типы контента (multipart / form-data, application / x-www-form-urlencoded или нет)

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);

По результатамURL php :

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

Разве это не должно работать? Знаете, что я могу делать неправильно?

1 Ответ

1 голос
/ 07 апреля 2019

multipart / form-data

new FromData - это multipart / form-data с границей (https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html). вы не должны устанавливать какие-либозаголовок типа содержимого. Заголовок будет установлен браузером. Вы можете получить доступ к данным с помощью $_POST или $_REQUEST.

application / x-www-form-urlencoded

если вы используете application / x-www-form-urlencoded, вы должны отправить закодированный URL-адрес.

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));

вы можете получить доступ к данным с помощью $ _POST или $ _REQUEST

application / x-www-form-urlencoded имеет меньше накладных расходов.

...