Liron Ilayev Liron Ilayev - 2 months ago 7
Javascript Question

$scope.someVariable can't be touched

I'm new to AngularJS so my mistake might be anywhere in my code and I can't find it. I'm using

$HTTP GET
method to retrieve data that's located in some server /page. After retrieving that data (which is JSON) I want to play with that string to retrieve the data properly, like name: number: and so on. But the thing is once I put that data into
$scope.listOfCompanyUsers
I can't touch it. If I try to
$scope.listOfCompanyUsers.slice(..)
or if I try any other string function on that object my entire webpage crashes. I "
alert()
"'d the
$scope.listOfCompanyUsers
and the result is:

<pre>[
{
"admin": true,
"id": 123,
"username": "someName",
"last_name": "someLastName",
"name": "John Doe"
}
]</pre><br>


What I wanted to do is remove the pre and br tags from that string so I have a pure JSON string that I could play with but again any function I try on
$scope.listOfCompanyUsers
crashes my site. What do I do? I tried var
someOtherVariable = $scope.listOfCompanyUsers
but that variable doesn't work later. I'm adding parts of my code because my mistake might be somewhere else.

Controller:

$http({
method: 'GET',
url: '/someURL'
}).then(function successCallback(response) {
$scope.listOfCompanyUsers = response.data;
},
function errorCallback(response) {
alert(response.status);
});


Later on the same controller:

.
.
$scope.someFunction = function () {
.
.
else {
alert("Maximum of 9 other passengers!");
alert($scope.listOfCompanyUsers);
// In this alert I could see the $scope.listOfCompanyUsers as mentioned above
}
};


My target right now is to have a var objectOfUsers =
[{admin: true, id:123, username: "name", last_name: "test", name: "something"}, {next user.}, .]
but because I can't touch the
$scope.listOfCompanyUsers
I'm stuck.

Answer

The problem is that the server is adding some extra tags to the response that shouldn't be in there:

<pre>[
  {
    "admin": true, 
    "id": 123, 
    "username": "someName", 
    "last_name": "someLastName", 
    "name": "John Doe"
  }
]</pre><br>

Edit your server-side code, to remove: <pre> and </pre><br>.

Then the call will work.

Comments