0

I am using angular js and php. I have an array to post in my controller from js file. I have converted my array to JSON and tried to pass the data like below

var update = {
                method: 'POST',
                url: apiPoint.url + 'up.php',
                 headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
               params :{
                        alldatas: JSON.stringify($scope.alldata) ,

                        section : 'A',
               }

By doing this I am getting 414 status error code. The url is too long. So I have tried JSONC to pack my data.. I use jsonc.min.js and have updated my code as below.

var update = {
                method: 'POST',
                url: apiPoint.url + 'up.php',
                 headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
               params :{
                        alldatas: JSONC.pack($scope.alldata) ,

                        section :'A',
               }

Now my data is passing through url and in my controller I get the data. But I can't unpack the data. Please help me to unpack the data.

Jonnny
  • 4,939
  • 11
  • 63
  • 93
athi
  • 123
  • 9

1 Answers1

0

I suppose you are using the standard $http component, and the mentioned JavaScript object stands for its configuration object.

You should pass JSON data through POST, i.e. the request body, because the request URI length is limited. The params option is serialized into GET. So you need to move your data (alldatas) into data option:

var req = {
  method: 'POST',
  url: apiPoint.url + 'up.php',
  data: { alldatas: $scope.alldata },
  // you might even stringify it
  //data: JSON.stringify({ alldatas: $scope.alldata }),
  headers: { 'Content-Type': 'application/json;charset=utf-8' },
  params: { section : 'A' }
};

$http(req).then(function (response) {
  console.log(response.data);
});

For the application/x-www-form-urlencoded content type you should build a query string:

var req = {
  method: 'POST',
  url: apiPoint.url + 'up.php',
  data: 'alldatas=' + encodeURIComponent(JSON.stringify($scope.alldata)),
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  params: { section : 'A' }
};

The $_POST superglobal is filled only for application/x-www-form-urlencoded and multipart/form-data content types. The other content types are available via the php://input stream:

if (!empty($_POST)) {
  $obj = $_POST;
} else {
  $json = file_get_contents('php://input');
  $obj = json_decode($json);
}
if ($obj) {
  header('Content-Type: application/json');
  echo json_encode($obj);
}
exit();
Community
  • 1
  • 1
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • no. in up.php am getting only section. alldatas is undefined – athi Oct 20 '16 at 09:01
  • @athi, have you tried to check if it is there: `var_dump($_POST);`? Have you checked the HTTP request headers - does it actually send the POST data to the server? Maybe some kind of cache is in effect. Try to send a test string instead of `JSON.stringify(...)`. Maybe the argument (`$scope.alldata`) has invalid data (or is just an `"undefined"` string). The main idea is to move the data intended to be sent to the server into the `data` section. – Ruslan Osmanov Oct 20 '16 at 09:08
  • @athi, added samples for both `application/x-www-form-urlencoded` and `application/json` – Ruslan Osmanov Oct 20 '16 at 10:47