This question already has an answer here:
这个问题在这里已有答案:
- Issue reading HTTP request body from a JSON POST in PHP 1 answer
- 在PHP 1答案中从JSON POST读取HTTP请求正文的问题
I have an AJAX call that is not sending information over to the server from the data parameter. I confirmed it's being recognized as a POST request with my PHP code below. You will notice my var_dump($_POST) does not contain the 'myData' data. I'm not sure where to go from here.
我有一个AJAX调用,它没有从data参数向服务器发送信息。我确认它被认为是一个POST请求,下面是我的PHP代码。你会注意到我的var_dump($ _ POST)不包含'myData'数据。我不知道从哪里开始。
JavaScript
JavaScript的
$.ajax({
method : 'POST',
contentType: 'application/json; charset=utf-8',
url : myURL,
data : {
'myData' : 'myData'
},
async : true,
success: function (results) {
console.log('here are the results: ' + results);
},
error: function (req, msg, obj) {
console.log('An error occured while executing a request');
console.log('Error: ' + msg);
}
});
PHP
PHP
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo var_dump($_POST);
}
Console.log
CONSOLE.LOG
here are the results: array(1) { ["phpURI"]=> unicode(33) "php/main.php?
function=myFunction" }
Thanks!
谢谢!
1 个解决方案
#1
-1
Your AJAX request is posting JSON data due to the Content-Type: application/json; charset=utf-8
header.
由于Content-Type:application / json,您的AJAX请求正在发布JSON数据; charset = utf-8标题。
If you just delete that line, $.ajax
defaults to application/x-www-form-urlencoded; charset=UTF-8
, which should cause PHP to put it into $_POST
as key-value pairs.
如果您只是删除该行,$ .ajax默认为application / x-www-form-urlencoded; charset = UTF-8,这会导致PHP将其作为键值对放入$ _POST。
#1
-1
Your AJAX request is posting JSON data due to the Content-Type: application/json; charset=utf-8
header.
由于Content-Type:application / json,您的AJAX请求正在发布JSON数据; charset = utf-8标题。
If you just delete that line, $.ajax
defaults to application/x-www-form-urlencoded; charset=UTF-8
, which should cause PHP to put it into $_POST
as key-value pairs.
如果您只是删除该行,$ .ajax默认为application / x-www-form-urlencoded; charset = UTF-8,这会导致PHP将其作为键值对放入$ _POST。