gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:53:03 25 4
gpt4 key购买 nike

尝试使用一些变量和长 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 :(

我尝试了不同的内容类型(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

这不是应该行得通吗?你知道我可能做错了什么吗?

最佳答案

多部分/表单数据

new FromData 是带有边界 ( https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html ) 的 multipart/form-data。您不应设置任何内容类型 header 。 header 将由浏览器设置。您可以使用 $_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 的开销较小。

关于javascript - 无法访问 $_POST 或 $_REQUEST 或 'php://input' - 全部不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55557982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com