AngularJS $http.post and PHP


AngularJS $http.post and PHP

我使用AngularJS $http。但是我还想在URL(或其他方式)中包含某种类型的数据,以指定该对象包含什么以及在PHP文件中解析它的位置。任何想法如何我可以发送一个变量类似于GET进入AngularJS post所以在PHP中我可以路由对象到正确的if语句进行解析?

下面是我的AngularJS代码:
$scope.saveSchool=function(){
  var schoolData = angular.copy($scope.schoolsModal);
  $http.post('data/data.php?schoolModal=save', schoolData).error(function(){
    console.log('Did not post data to data.php');
  });
};

这里是我在data. PHP文件中的PHP代码,希望接收和路由对象到正确的if语句进行解析并最终保存数据。

if (isset($_GET["schoolModal"])) {
  if ($_GET["schoolModal"] == "save") {
   $postdata = file_get_contents("php://input");
   $request = json_decode($postdata);
   echo "Acknowledged";
   echo $postdata;
 }

}

这不起作用,因为它不会抛出任何错误或返回任何东西(PHP)。这确实有效,但我无法在php文件中路由对象(我必须为这个angularjs json对象创建一个单独的php文件)。

$scope.saveSchool=function(){
  console.log('saveSchool function initiated');
  var schoolData = angular.copy($scope.schoolsModal);
  $http.post('data/data.php', schoolData).error(function(){
    console.log('Did not post data to data.php');
  });
};

PHP脚本,将需要在它自己的文件,因为我想最终有多个post函数和解析数据,因为它收到:

if($_SERVER['REQUEST_METHOD']=='POST'){
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
echo $postdata;

}

PHP代码工作得很好,但我无法将数据路由到需要去的地方。我不想发送所有的帖子,如果声明。有什么想法吗,我是刚接触AngularJS和PHP的新手,正在努力学习这些

考虑我做的一个项目中的这个例子。它只有一个参数" status ",但显然你可以把所有的参数添加到一个对象中,就像这样

postFavorite: function(id, type, record, resource) {
  var xsrf = $.param({status: record.prop});
  $http({
    method: 'POST',
    url: this.api_base + '/favorites/'+type+'/'+id,
    data: xsrf,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},                 
    withCredentials: true
  }).success(function(data, status, headers, config){
    if(data.success){
      console.log("Success:", data, status, headers, config);
    }
  });
},