0

I must be missing something... because I'm really a noob at this.

I have this angularjs data:

$scope.todoList = [
      { text: 'Check me out' },
      { text: 'Lorem ipsum dolor sit amet, possit denique oportere at his, etiam corpora deseruisse te pro' },
      { text: 'Ex has semper alterum, expetenda dignissim' },
    ];

And I wanted to put it in a mysql table instead. I created the table and then tried to get table data:

JS:

$http({method:'POST',url:'process.php'})
          .then(
            function successCallback(response) { $scope.todoList = response.data;}, 
            function errorCallback(response) {$scope.todoList = "ERROR!";});

PHP:

    $sql = "SELECT * from `todolist`";
    $result = $conn->query($sql);
    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $data[] = array("text" => $row['text']);
    }
    echo json_encode($data);

When I manually try the PHP the output is:

[{"text":"Check me out"},{"text":"Lorem ipsum dolor sit amet, possit denique oportere at his, etiam corpora deseruisse te pro"},{"text":"Ex has semper alterum, expetenda dignissim"}]

Does someone know where the problem is?

2
  • 1
    What's your problem exactly? Commented Mar 11, 2017 at 22:23
  • Your response data type is not valid json data type, and assigns response-string without parsing to $scope.todoList; use JSON.parse(response.data) Commented Mar 11, 2017 at 22:28

2 Answers 2

0

I think your problem is about reading and assigning value of response data; so I've commented:

Your response data type is not valid json data type, and assigns response-string without parsing to $scope.todoList; use JSON.parse(response.data)

and suggest these topics (maybe helpful):

If you have problem with Updating after AJAX call:

more related to this problem:


If your problem is none of these topics, let us know to solve it.

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

Comments

0

I tried JSON.parse(response.data) but no success. I'll try to elaborate a bit more:

This code worked fine:

function getRandomColor() {
  var i = Math.floor(Math.random() * (colors.length - 1));
  return colors[i];
}

$scope.todoList = [
  { text: 'Check me out' },
  { text: 'Lorem ipsum dolor sit amet, possit denique oportere at his, etiam corpora deseruisse te pro' },
  { text: 'Ex has semper alterum, expetenda dignissim' },
];

$scope.todoList.forEach(function(item) {
  item.color = getRandomColor();
});

But now this one gives an error in console:

TypeError: Cannot read property 'forEach' of undefined

function getRandomColor() {
  var i = Math.floor(Math.random() * (colors.length - 1));
  return colors[i];
}

$http({method:'POST',url:'process.php',data:{function: "todolist"}})
          .then(
            function successCallback(response) {$scope.todoList = response.data;}, 
            function errorCallback(response) {console.log("ERROR!");});

$scope.todoList.forEach(function(item) {
  item.color = getRandomColor();
});

It seems like it doesn't wait for the ajax results... But I don't really know.

SOLVED putting foreach function inside successCallback function.

2 Comments

Solved putting the forEach function inside the successCallback. Thanks everyone!

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.