简体   繁体   English

AngularJS $ http.post和PHP

[英]AngularJS $http.post and PHP

I'm using AngularJS to $http.post an object, but I also want to include some kind of data in the URL (or some other way) to specify what this object contains and where to parse it in the PHP file. 我使用AngularJS来$ http.post一个对象,但我也想在URL中包含某种数据(或其他方式)以指定该对象包含什么以及在PHP文件中的解析位置。 Any idea how I can send a variable similar to GET into the AngularJS post so in PHP I can route the object to the correct if statement for parsing? 知道如何将类似于GET的变量发送到AngularJS帖子中,以便在PHP中可以将对象路由到正确的if语句进行解析吗?

Here is my AngularJS code: 这是我的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');
  });
};

And here is my PHP code in data.php file in hopes to receive and route the object to the correct if statement for parsing and eventually saving the data. 这是我在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;
 }

} }

This doesn't work, as it doesn't throw any errors or return anything (PHP). 这是行不通的,因为它不会引发任何错误或返回任何内容(PHP)。 This does work, but I'm unable to route the object in the php file (i would have to create a separate php file just for this angularjs json object). 确实可以,但是我无法在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 Script that would need to be in it's own file, as I want to eventually have multiple post functions and parse the data as it's received: PHP脚本需要放在其自己的文件中,因为我最终希望拥有多个发布功能并在收到数据时解析数据:

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

} }

That PHP code works just fine, but i'm unable to route the data to where it needs to go. 那个PHP代码可以正常工作,但是我无法将数据路由到需要去的地方。 I don't want to send ALL posts to that if statement. 我不想将所有帖子发送到该if语句。 Any ideas, I'm new to AngularJS and PHP and doing my best to pick this up. 任何想法,我都是AngularJS和PHP的新手,并尽我最大的努力来学习。

Consider this example from a project I did. 考虑我做过的一个项目中的这个例子。 It only has 1 param 'status', but obviously you can add all of your params to an object like that 它只有1个参数“状态”,但是显然您可以将所有参数添加到这样的对象中

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);
    }
  });
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM