angularjs-将对象(JSON数据)数组发布到PHP页面 [英] angularjs - POST an array of objects(JSON data) to a PHP page

查看:117
本文介绍了angularjs-将对象(JSON数据)数组发布到PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关JSON数据的示例:

An example of how my JSON data is like:

$scope.a = [{
            "email": "keval@gmail",
            "permissions": {
                "upload": "1",
                "edit": "1"
            }
        }, {
            "email": "new@aa",
            "permissions": {
                "upload": "1",
                "edit": "1"
            }
        }];

我要发布相同的内容,这是我的方法:

I want to post the same, and here's my approach:

$http({
    method: 'POST',
    url: 'backend/savePermissions.php',
    data: {
        mydata: $scope.a
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
})
.success(function(data) {
    console.log(data);
});

PHP将接受请求并做出响应:

And the PHP is to take the request and respond:

echo $_POST['mydata'];

我在调用之前尝试了JSON.stringify,并在回显时尝试了json_decode.仍然没用.一直在尝试我能想到的所有可能性,以及在网络上找到的内容/其他问题,但仍无法正常工作.

I tried JSON.stringify before the call and json_decode while echoing it back; still didn't work. Been trying all the possibilities I can think of, and what I find on the web/other questions, but still not working.

推荐答案

我为您制作了plnkr http://plnkr.co/edit/K8SFzQKfWLffa6Z4lseE?p=preview

I've made plnkr for you http://plnkr.co/edit/K8SFzQKfWLffa6Z4lseE?p=preview

$scope.postData = function () {
    $http.post('http://edeen.pl/stdin.php', {user:$scope.formData}).success(
      function(data){
        $scope.response = data
      })
  }

如您所见,我在发送原始JSON而不格式化它,然后在php中

as you can see I'm sending a raw JSON without formating it, then in php

<?php
  echo file_get_contents('php://input');

我直接读取JSON并回显它,但是您可以做任何您想做的事

I read the JSON directly and echo it but you can do whatever you want

http://php.net/manual/en/wrappers.php上了解有关php://input的更多信息. php

read more about php://input here http://php.net/manual/en/wrappers.php.php

我为REST服务使用了很长时间,以避免多次将JSON转换为字符串

I was using it for a long time for REST services to avoid transforming JSON to string to many times

这篇关于angularjs-将对象(JSON数据)数组发布到PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆