5

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

And the PHP is to take the request and respond:

echo $_POST['mydata'];

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.

4
  • I think that if the value for the attribute in the data object is undefined, then it is not even sent I tried to add null and could see it in the request headers at the console. plnkr.co/edit/IqQY5dJoS5VMObsYsbha?p=preview Commented Mar 4, 2015 at 10:05
  • Sorry, but I doubt if I followed what you said. Anyway, I got a solution below. Thank you. Commented Mar 4, 2015 at 10:38
  • @keval are you trying to make webservice using AngularJS and PHP ? If yes then I want to know if your json output is parse the JSON validation in jsonlint.com or not, because I am trying to do the same (webservice in AngularJS + PHP) and I am getting whole html in JSONLint parsing Commented Apr 10, 2015 at 12:27
  • Yes, my JSON is valid as per JSONLint. And I am not getting you, what do you mean by "getting whole html"? Could you post your code in a new question and give me the link? Or just show me the code and I will try to see what's wrong. Commented Apr 10, 2015 at 13:18

3 Answers 3

6

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

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

<?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 http://php.net/manual/en/wrappers.php.php

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great! That one line is it, I'm getting the data in response; everything looks good. Thank you.
2

I use this, with which I can send Array JSON:

var array = {}
array['key1'] = value1;
array['key2'] = value2;

$http.post(URL, array)
  .success(function(data){
  })
  .error(function(){

});

Comments

1

try using $httpParamSerializer or $httpParamSerializerJQLike

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.