AngularJs AJAX POST到PHP
这是我第一次使用从AngularJs到PHP的POST 但是,谷歌是我的朋友——至少我是这么想的 根据谷歌的几项发现,这应该可以很好地工作: AngularJs:AngularJs AJAX POST到PHP,php,angularjs,json,post,Php,Angularjs,Json,Post,这是我第一次使用从AngularJs到PHP的POST 但是,谷歌是我的朋友——至少我是这么想的 根据谷歌的几项发现,这应该可以很好地工作: AngularJs: var data = $.param({ json: JSON.stringify({ userName: $scope.registrationData.userName, email: $scope.registrationData.email,
var data = $.param({
json: JSON.stringify({
userName: $scope.registrationData.userName,
email: $scope.registrationData.email,
password : forge_sha256($scope.registrationData.password)
})
});
var url = HOST + 'api/register.php?debug';
console.log('Register at ' + url);
$http.post(url, data)
PHP
但是,这两个ChromePhp::log
在浏览器控制台中显示:
%cF:\DropBox\programs\Xampp\htdocs\api\register.php : 17
log.js:137 API: JOSON data = json=%7B%22userName%22%3A%22n%22%2C%22email%22%3A%22e%22%2C%22password%22%3A%22148de9c5a7a44d19e56cd9ae1a554bf67847afb0c58f6e12fa29ac7ddfca9940%22%7D
log.js:81 %cF:\DropBox\programs\Xampp\htdocs\api\register.php : 18
log.js:137 API: decoded data =
解码后的JSON为空:-(因此,我的编码或解码出现了问题
我离它太近了,看不见它-谁能认出我的d'oh?你可以这样试试:
var data = $.param({userName:$scope.registrationData.userName,
email:$scope.registrationData.email,
password:forge_sha256($scope.registrationData.password)});
var config = {headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'}}
$http.post("url/to/ur/file",data,config)
.success(function(response) {
$scope.data = response;
});
PHP:您可以访问以下数据:
$uesrName = $_POST['userName'];
$email = $_POST['email'];
$password = $_POST['password'];
您将获得更多信息:$http如果您想以JSON格式发送数据,它应该简单如下:
var data = {
userName: $scope.registrationData.userName,
email: $scope.registrationData.email,
password: forge_sha256($scope.registrationData.password)
};
...
$http.post(url, data);
您还可以通过检查Chrome开发者工具中的“网络”选项卡来检查Angular应用程序发送的内容。这样,您可以确定这是客户端问题还是服务器端问题。这是在假设OP不需要JSON?很好的观点(+1),但我并不在乎,只要它能工作就行
var data = {
userName: $scope.registrationData.userName,
email: $scope.registrationData.email,
password: forge_sha256($scope.registrationData.password)
};
...
$http.post(url, data);