file_get_contents(“php:// input”)后ajax后空
我很犹豫要问,但我还没有找到任何解决方案。在ajax函数中发送php文件中的数据之后,当我尝试捕获php文件中的数据时,数据丢失。但是,昨天我发现了一个没有问题的类似功能。 这是AJAX部分:file_get_contents(“php:// input”)后ajax后空
$http({
url: 'PDO/Companion.php',
data: {
Companion : Companion,
Companion : statut
},
method: 'POST',
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
}).then(function (response) {// on success
$scope.name=undefined;
$http.get('js/controller/upPartenaire.php').success(function(data) {
$scope.result = data;
console.log(data);
}), function(msg){
console.log("ça")
alert(msg);
};
,而PHP的一部分:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
echo json_encode($request);
在项目中,我使用PHP只针对AJAX部分做SQL queryI也赶上与$ _ POST,但没有数据
您需要设置的内容类型,以JSON和使JSON字符串化像
$http({
url: 'PDO/Companion.php',
data: JSON.stringify({
Companion : Companion,
Companion : statut
}),
method: 'POST',
headers : {'Content-Type':'application/json; charset=utf-8'}
}).then(function (response) {// on success
$scope.name=undefined;
$http.get('js/controller/upPartenaire.php').success(function(data) {
$scope.result = data;
console.log(data);
}), function(msg){
console.log("ça")
alert(msg);
};
我试过你的解决方案,但没有改变 –
什么是错误? –
如果你想使用'x-www-form-urlencoded',最好的方法是使用'$ _POST ['Companion']'。另一件事是你发送同伴两次 –
这应该对你有所帮助。考虑更改php代码:
header("Content-Type: application/json");
parse_str(file_get_contents('php://input', false , null, -1 ,
$_SERVER['CONTENT_LENGTH']), $postdata);
echo json_encode($postdata);
感谢您的帮助,我尝试你的解决方案,但它不运行,我有两个错误信息: 注意:未定义的索引:CONTENT_LENGTH 和 警告:json_decode( )期望参数1是字符串,第5行中给出的数组 –
$ _SERVER ['CONTENT_LENGTH']应该可用,如果您发出发布请求。此外,我会编辑我的答案更新的解决方案。 –
好吧,也许PHP的部分不是问题,但阿贾克斯部分。 –
您是否已完成尽职调查并使用浏览器开发人员控制台和网络选项卡调试了ajax调用?你是否积极发送数据,并且没有控制台错误? – IncredibleHat
您还应该包含'var Companion'和'statut'定义的代码,并解释您为什么试图将“Companion”的数据键名发送两次。最后,如果您加载了jquery库,那么为什么不使用'$ .post'来简化很多? [JQuery的.post](https://api.jquery.com/jquery.post/) – IncredibleHat