2017-03-11 12 views
0

私は何かを見逃しているに違いない...私は本当にこれのノブです。AngularJS to Mysql

は、私は、これはデータangularjs持っている:

$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' }, 
    ]; 

そして、私が代わりにmysqlのテーブルに入れたかったです。私は、テーブルを作成して、テーブルのデータを取得しようとした:

JSを:

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

PHP:私は手動で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); 

出力は次のとおりです。

[{"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"}] 

誰かが問題の原因を知っていますか?

+1

あなたの問題は何ですか? –

+0

あなたの応答データ型は[valid json data type]ではありません(http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type)、 ' $ scope.todoList'; 'JSON.parse(response.data)'を使う – MohaMad

答えて

0

あなたの問題は約で、応答データの値を読み込んで割り当てていると思います。;私はコメントしました:

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

をと提案し、これらのトピック(多分便利):

あなたは更新で問題が発生した場合AJAXコール後

この問題への複数の関連:


問題がこれらのトピックに該当しない場合は、解決するためにお知らせください。

0

私はJSON.parse(response.data)を試みましたが、成功しませんでした。 私はもう少し詳しく説明しようとするでしょう:

このコードはうまく働いた:

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

しかし今この1つは、コンソールにエラー与える:

TypeError例外を:のプロパティを読み取ることができません「のforEach」を定義されていません

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

これは、Ajaxの結果を待っているようです...しかし、私は本当に知りません。

SOLVED foreach関数をsuccessCallback関数内に配置しました。

+1

forEach関数をsuccessCallbackの中に置くことを解決しました。みんな、ありがとう! –

+0

良い編集と[応答がangeljsの$ http要求から来るまで待つ方法](http://stackoverflow.com/questions/18421830/how-to-wait-till-the-response-comes-from) -http-request-in-angularjs) – MohaMad