2016-03-25 11 views
2

Basit veritabanı arama motoru AngularJS içinde basit bir arama motoru yapmaya çalışıyorum. İstemcim ile sunucum arasındaki iletişim ile ilgili bir sorunum var. W3schools kılavuzunu http://www.w3schools.com/angular/angular_sql.asp takip etmeye çalışıyorum. İşte AngularJS

home.php vücudu:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  
</script> 
</head> 

<body> 
<div ng-app="myApp" ng-controller="customersCtrl"> 
<table> 
    <tr ng-repeat="x in names"> 
     <td>{{ x.Name }}</td> 
    </tr> 
</table> 
</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', function($scope, $http) { 
    $http.get("server.php") 
    .then(function (response) {$scope.names = JSON.parse(response.data.records);}); 

}); 

</script> 

ve burada server.php var: ben bu hatayı alıyorum, ancak

<?php 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 
error_reporting(2); 

$conn = new PDO('mysql:host=localhost;dbname=mygaloo;charset=utf8', 'root', ''); 
$result = $conn->query("SELECT * FROM associations"); 
$outp = ""; 

while($donnees = $query->fetch()) 
{ 
    if ($outp != "") {$outp .= ",";} 
    $outp .= '{"Name":"' . $donnees["nom"] . '"}'; 
} 

$outp ='{"records":['.$outp.']}'; 
$conn->close(); 


echo($outp); 
?> 

: angularjs: 12520SyntaxError: 0 pozisyonunda JSON'da beklenmedik bir simge, herhangi bir fikir?

+0

Sunucu.php'de hata olmadığından emin olun. Bir tane var .. – Rayon

+0

{{x.Name}} ile beyaz bir sayfa alıyorum. –

+0

'error_reporting (2);' ve test .. – Rayon

cevap

0

Tamam anladım. Arun Shinde cevap haklıydı, ancak sorun geliyor: Ben işte MySQLi kullanarak açıkçası, işe gitmiyor PDO kullanıyorum

while($rs = $result->fetch(MYSQLI_ASSOC)) { 

. Yani burada çalışan tam kod. home.php:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  
    </script> 
</head> 

<body> 
    <div ng-app="myApp" ng-controller="customersCtrl"> 
    <table> 
     <tr ng-repeat="x in names"> <td>{{ x }}</td> </tr> 
    </table> 
    </div> 

    <script> 
    var app = angular.module('myApp', []); 
    app.controller('customersCtrl', function($scope, $http) { 
      $http.get("server.php") 
      .then(function (response) { 
       $scope.names = response.data; 
      }).then(function(response){ 
       console.log(response) 
      }) 

    }); 
    </script> 
</body> 

server.php: sabır herkes için

<?php 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 

$conn = new PDO('mysql:host=localhost;dbname=mygaloo;charset=utf8', 'root', ''); 
$result = $conn->query("SELECT * FROM associations"); 
$outp = []; 
while($rs = $result->fetch()) { 
    if ($outp != "") 
    array_push($outp,$rs["nom"]); 
} 
$outp =json_encode($outp); 
$conn = null; 
echo($outp); 
?> 

Çok teşekkürler!

+0

Harika. Bu yüzden sana tüm PHP kodunu verdim. Eğer çözümü bulursanız, cevabı kabul etmeyi deneyin; Bunun için tüm çabalardan sonra sadece. :-) –

0

İşte açısal kod

burada
<script> 
     var app = angular.module('myApp', []); 
     app.controller('customersCtrl', function($scope, $http) { 
       $http.get("server.php") 
       .then(function (response) { 
        $scope.names = response.data; 
       }).then(function(response){ 
        console.log(response) 
       }) 

     }); 
    </script> 

ve PHP kodu olmasıdır. Umarım size yardımcı olur.

<?php 
    header("Access-Control-Allow-Origin: *"); 
    header("Content-Type: application/json; charset=UTF-8"); 

    $conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', ''); 
    $result = $conn->query("SELECT * FROM associations"); 
    $outp = []; 
    while($rs = $result->fetch(MYSQLI_ASSOC)) { 
    if ($outp != "") 
    array_push($outp,$rs["nom"]); 
    } 
$outp =json_encode($outp); 
$conn = null; 
echo($outp); 
?> 
+0

Konsol.log "undefined" değerini döndürür –

+0

Bunu yoksay. ** $ scope.names = response.data; ** 'da herhangi bir başarı elde ediyor musunuz? Bunun dışında bir hata var. İşte yazdıracağınız HTML. ** \t \t \t \t \t {{adı}} pozisyonda 0' –

+0

'beklenmeyen belirteç