$http()不';t通过方法'发送数据;邮政';到PHP服务器
当我通过方法post将数据发送到php文件时遇到了问题,下面是一个示例:$http()不';t通过方法'发送数据;邮政';到PHP服务器,php,angularjs,http,request,Php,Angularjs,Http,Request,当我通过方法post将数据发送到php文件时遇到了问题,下面是一个示例: function SimuladorService ( $http, config ) { $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;"; $http.defaults.headers.post["Accept"] = "application/x-www-form-urle
function SimuladorService ( $http, config ) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";
$http.defaults.headers.post["Accept"] = "application/x-www-form-urlencoded";
const post = function(dados) {
return $http({
method: 'POST',
url: "example.php",
data: {
nome:'almir',
sobrenome: 'teste'
},
transformRequest : function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
})
}}
example.php
<?php
echo json_encode($_POST);
如果问题出在angular方面,请尝试以下代码:
$scope.post = function(dados){
console.log(dados)
$http({
method: 'POST',
url: "example.php",
data: {
nome:'almir',
sobrenome: 'teste'
},
withCredentials: true,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
})
.then(function successCallback(data) {
console.log(data)
}, function errorCallback(response) {
console.log(response);
});
}
html:单击
检查您在控制台.log中得到的信息如果问题仍然存在,则可以找到问题的原因要将数据发布到PHP API,只需使用默认值:
var data = {
nome:'almir',
sobrenome: 'teste'
};
function post(data) {
var url = "example.php";
return $http.post(url,data);
}
post(data).then(function(response) {
console.log(response.data)
$scope.data = response.data;
}).catch(function(response) {
console.log(response);
throw response;
})
在PHP服务器端:
<?php
$data = json_decode(file_get_contents('php://input'), true);
echo file_get_contents('php://input');
问题出在服务器端还是前端?例如,我创建了一个用于测试的变量,它成功了。问题是通过POST方法发送数据。我已经用postman进行了测试,并实现了file\u get\u contents('php://input“)在当地的环境不起作用。我不知道为什么。这和我创造的一样。问题是php文件不接收$\u post并返回我的$\u post数据。我已经用postman进行了测试,并实现了文件$\u get\u内容('php://input“)在当地的环境不起作用。我不知道为什么。我已经和邮递员一起测试过了,并且实现了文件获取内容php://input“)在当地的环境不起作用。我不知道为什么。一定要使用内容类型:application/json
。不要覆盖AngularJS默认值。