Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

at start, I come with a JSON array, like this one :

[
    {
        "login":"Admin",
        "admin":true,
        "root":true
    },
    {
        "login":"PMO",
        "admin":true,
        "root":false
    }
]

I want to overwrite the content of this JSON array by another, which is into the variable $scope.users I first send it to a PHP file from a controller :

$http.post('php/users/deleteUser.php', $scope.users);

which overwrites the old json file

<?php
    $data = file_get_contents("php://input");
    file_put_contents('../users/users.json', $data);
?>

But when I open the file users.json, the content is malformed :

0%5Blogin%5D=Admin&0%5Badmin%5D=true&0%5Broot%5D=true&0%5B%24%24hashKey%5D=01R&1%5Blogin%5D=PMO.....

I don't manage to get where it comes from. I've tried all possible combinations with JSON.parse, JSON.stringify, json_encode, json_decode. I also tried to write the $http request like this :

$http({
    method:"POST",
    url:'php/users/deleteUser.php',
    data:$scope.users,
    headers: {'Content-Type': 'application/json'}
});

But nothing changes the result. Any idea ?

share|improve this question
4  
php://input is RAW input. You'd have to urldecode() it first to conver the []{}" back to normal characters. Normally you should let PHP do it for you, which means sending over data=$json_string and the getting the decoded string from $_POST['data'] – Marc B Jul 8 '14 at 22:36
    
Ah thanks ! it's much better now ! however, the output is now like that in the json file : 0[login]=Admin&0... which is the json notation in php. I've tried with json_encode and json_decode, it doesn't looks like to change anything – Gullfaxi171 Jul 8 '14 at 22:45
    
that's not json. it's just a string of some sort. if it was json, it'd be more like {"login":"Admin",....} – Marc B Jul 8 '14 at 22:47
    
It is $scope.users is a json array. If I replace it by {"login":"Admin",....}, the output still sends me : login=Admin&admin=true&root=true – Gullfaxi171 Jul 8 '14 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.