【发布时间】:2019-08-28 16:35:16
【问题描述】:
尝试使用一些变量和长 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);
关于resultsURL php:
$docId = $_REQUEST['document_id'];
echo var_dump($docId); //returns NULL
这不是应该工作吗?你知道我做错了什么吗?
【问题讨论】:
标签: javascript post xmlhttprequest