Keval Keval - 3 months ago 26
JSON Question

angularjs - POST an array of objects(JSON data) to a PHP page

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:

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

And the PHP is to take the request and respond:

echo $_POST['mydata'];

I tried
before the call and
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.


I've made plnkr for you

$scope.postData = function () {
    $'', {user:$scope.formData}).success(
        $scope.response = data

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

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

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

read more about php://input here

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