我对AngularJS相当陌生,我正在尝试执行http服务将数据发送到php文件以输入MySQL。我已经能够得到这个工作使用jQuery和$.ajax
函数,所以我确信我的PHP是对的。
ng-click
绑定到a
元素routeReloader(data)
上的函数。当我console.log
routerloader函数中的参数时,我得到了正确的参数。我还收到了一个提示框,提示"成功"。但是,它没有更新MySQL。
userApp.controller('photoController', ['$scope','$location','$log','$route','$http', function($scope,$location,$log,$route,$http){
$scope.routeReloader = function(parameters){
$http.post('centralcommand.php', {id: parameters}).success(function(data,status,headers,config){
alert('success');
}).error(function(data,status,headers,config){
alert('failure');
});
};
}]);
我使用$_POST['id']
在centralcommand.php文件中获取数据。
JQuery工作等效
$('#link-element').click(function(){
var REL = $(this).attr("rel");
var URL = 'centralcommand.php';
var dataString = 'id=' + REL;
$.ajax({
type: "POST",
url: URL,
data: dataString,
cache: false,
success : function() {
alert('success')
}
});
});
我能够找到一个基于评论的解决方案,因为传递的数据是JSON格式。我必须将JSON格式解码为PHP可以使用的格式:
PHP代码$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$photoID = $request->id;
用这个PHP代码转换JSON格式,你可以在你的SQL查询中使用它。注意,$request->id
使用的是AngularJS $http post请求中传递的相同的id
。