PHP 没有从 XMLhttprequest 接收数据 [英] PHP not receiving data from XMLhttprequest
问题描述
我正在将数据发送到 php 脚本,如下所示:
Hi I am sending data to a php script like so:
function ajax(url,data,success) {
var request = new XMLHttpRequest();
request.open("POST", url);
request.onreadystatechange = function(object) {
if(request.readyState === 3) {
success(request);
}
};
request.setRequestHeader("Content-Type","application/json")
request.send(data);
}
发送的数据是一个字符串化的 javascript 对象.该帖子确实有效,并且该对象显示在 chromes 开发工具的有效负载部分中.但是我发送给请求对象的 php 脚本是空的.php 脚本的内容类型设置为 json.
The data being sent is a stringifyed javascript object. The post definitely works and the object shows in the payload section in chromes dev tools. But the php script I am sending to request object is empty. The php script's content type is set to json.
推荐答案
听起来您遇到了一个众所周知的问题(这里有一些信息:PHP "php://input" vs $_POST)
Sounds like you're experiencing quite a well-known issue (some info here: PHP "php://input" vs $_POST)
您应该能够使用 file_get_contents('php://input') 访问数据
You should be able to access the data with file_get_contents('php://input')
这篇关于PHP 没有从 XMLhttprequest 接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!