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

I have sat behind this all day. I am building a plug in and i am including angular js.

So far all is well but i am not able to get a php file to read from database and pass values to angular js

file.php

<?php
global $wpdb;
$table_name = $wpdb->prefix . 'workout';

$results = $wpdb->get_results(" SELECT title FROM $table_name ");

$arr=[];
$i=0;

foreach($results as $r)
{
$arr[$i]['title'] = $r->title;
$i++;
}

echo json_encode($arr);
//echo '[{"title":"Demo Title"},{"title":"Demo Title"},{"title":"Demo Title"}]';

The way i know my code works is that when i uncomment the last part of my code above the code works. so for some reason the json serialisation is not working properly, also the commented code is what both echo json_encode($arr); and echo json_encode($results); return. Why is angularjs giving me nothing? What am I doing wrong here.

controller.js

var app = angular.module('myApp', []);

app.controller('workoutCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get(params.url)
        .success(function(data) {
            $scope.records = data;
        });
}]);

Table

echo '<div ng-app="myApp" ng-controller="workoutCtrl">';
echo '<table class="table">';
echo '<thead>';
echo '<tr >';
echo '<th>Title</th>';
echo ' </tr>';
echo ' </thead>';
echo ' <tbody>';
echo '  <tr ng-repeat = "x in records">';
echo '    <td>{{x.title}}</td>';
echo '  </tr>';
echo ' </tbody>';
echo ' </table>';
echo '</div>';
share|improve this question
    
Are you seeing a response in network of Chrome browser? – Dhiren Patel Mar 8 at 15:32
1  
Open that url in browser ... do you see proper data? What does an error handler tell you? Inspect the actual request in browser dev tools network for more clues – charlietfl Mar 8 at 15:33
    
Doesn't seem like you have a wordpress instance in that php file – charlietfl Mar 8 at 15:36
    
have you tried die() after echo, in wordpress one have to die() specially in wp_ajax_hookname hook. – shyammakwana.me Mar 17 at 20:26

You need to set response header in php

header('Content-Type: application/json');
echo json_encode($arr);
share|improve this answer
    
so why does it work with hard code data? This is good suggestion but isn't going to resolve problem – charlietfl Mar 8 at 15:37
    
Because you are sending a stringified response when sending in plain text. – Dhiren Patel Mar 8 at 15:38
    
and that's what json_encode returns also...string – charlietfl Mar 8 at 15:39
    
Please reverse the negative vote. Check out the related answer stackoverflow.com/questions/4064444/… – Dhiren Patel Mar 8 at 15:41

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.