Javascript 使用Angular$post和PHP Codeigniter进行请求和响应
我通过$post向Codeigniter发送数据时遇到问题。我正在使用这个JS代码:Javascript 使用Angular$post和PHP Codeigniter进行请求和响应,javascript,php,angularjs,codeigniter,Javascript,Php,Angularjs,Codeigniter,我通过$post向Codeigniter发送数据时遇到问题。我正在使用这个JS代码: $scope.user.first_name = 'first name'; $scope.user.last_name = 'last name'; $http({ method: 'POST', url: '/registration', data: {user: $.param($scope.user)}, headers: { '
$scope.user.first_name = 'first name';
$scope.user.last_name = 'last name';
$http({
method: 'POST',
url: '/registration',
data: {user: $.param($scope.user)},
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}).then(function successCallback(response) {
console.log(response)
}, function errorCallback(response) {
console.log(response);
});
我的PHP基于StackOverflow的另一个答案,但仍然不起作用:
echo file_get_contents('php://input');
var_dump($this->input->post());
,但通过这种方式,如果我说:
var_dump($this->input->post('user'));
我看到响应为空,与以下内容相同:
var_dump(json_encode($this->input->post()));
带或不带“用户”键。即使我使用:
var_dump($_POST);
对于响应,我得到了空数组
如何使用Codeigniter通过$post Angular service发送和获取数据?如果要以表单编码的形式发送数据,需要使用
$.param()序列化整个数据对象。
更改:
data: {user: $.param($scope.user)},
到
或使用
如果使用默认值$http
,则
$http.post( '/registration',$scope.user).then(...
在php中
$data = json_decode(file_get_contents('php://input'));
响应仍然是一个数据为NULL的对象。使用您的代码,但不是默认值。使用这个PHP
$data=json\u decode(file\u get\u contents('php://input'));var_dump(json_encode($data))
-它返回字符串NULL,不带json\u encode
就是NULL。我在开发工具中看到请求是通过浏览器发送的。因为您需要在两端使用相同的内容类型。如果您发送表单编码,则需要使用$\u POST
或$this->input->POST()
,如果您按照默认设置作为json发送,则使用文件获取内容
Thx很多:),现在使用变量转储($this->input->POST('user')代码>工作正常。
$data = json_decode(file_get_contents('php://input'));