无法访问$ _POST或$ _REQUEST或'php:// input'-全部都不为空

时间:2019-04-07 10:31:46

标签: javascript post xmlhttprequest

尝试使用一些变量和一个较长的JSON发出一个简单的xhr请求。当我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:(

我尝试了不同的Content-Type(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);

在resultsURL php 上:

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

这不行吗?你知道我可能做错了吗?

1 个答案:

答案 0 :(得分:1)

多部分/表单数据

new FromData是具有边界(https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html)的 multipart / form-data 。您不应设置任何内容类型标头。标头将由浏览器设置。您可以使用$_POST$_REQUEST访问数据。

应用程序/ 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具有较少的开销。