Amo*_*s47 4 php ajax http-post
我的JavasSript发送请求:
var jax = new XMLHttpRequest();
jax.open("POST", "http://localhost/some.php", true);
jax.setRequestHeader("Content-Type", "application/json");
jax.send(JSON.stringify(jsonObj));
jax.onreadystatechange = function() {
if(jax.readyState === 4) { console.log(jax.responseText); }
}
Run Code Online (Sandbox Code Playgroud)
现在我所有的PHP都是:
print_r($HTTP_RAW_POST_DATA);
print_r($_POST);
Run Code Online (Sandbox Code Playgroud)
原始发布数据的输出是对象字符串,但是post数组是空的.
{"name" : "somename", "innerObj" : {} ... }
Array
(
)
Run Code Online (Sandbox Code Playgroud)
我需要以适当的格式获取$_POST变量,而jquery不是一个选项.
是的,因为user1091949发布了我的评论作为答案,这也是同样的事情,所以OP可以选择批准的答案(如果有效):
$json = json_decode(file_get_contents('php://input'));
Run Code Online (Sandbox Code Playgroud)
此时,$json将是...的实例stdClass.如果您更喜欢关联数组,只需传递第二个参数即可json_decode('{"json":"string"}', true);
BTW:千万不要,曾经使用的禁止错误抑制死亡:@.错误有助于你,而不是惹恼你......