Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm trying to fetch some data from a mysql database using php, and displaying it inside of angular. I am using WAMP server to host everything locally.

However, the response I get is in HTML insead of being JSON.

Here are my code snippets:

app.js

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

incomeTracker.controller('mainCtrl', ['$scope','$http', function($scope,$http) {


console.log('salut');

 $scope.addToCard = function() {
    $scope.card = $scope.card - (-$scope.cardAdd);
  };

  $http.get("http://localhost/incomeTracker/php/preluare.php")
        .success(function (response) 
        {
            console.log(response);
        }); 

}]);

preluare.php

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

$conn = new mysqli("localhost", "root", "", "valori");

$result = $conn->query("SELECT cardMoney, cashMoney, voucherNumber, voucherValue FROM valori");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"cardMoney":"'  . $rs["cardMoney"] . '",';
    $outp .= '"cashMoney":"'   . $rs["cashMoney"]        . '",';
    $outp .= '"voucherNumber":"'. $rs["voucherNumber"]     . '"}'; 
    $outp .= '"voucherValue":"'. $rs["voucherValue"]     . '"}'; 
}
$outp ='{"records":['.$outp.']}';
console.log('sunt aici');
$conn->close();

echo($outp);
?>

index.html

<!DOCTYPE html>
<html ng-app="incomeTracker">
<head>
    <title>Income Tracker</title>

    <script src="node_modules/angular/angular.js"></script>
    <script src="js/app.js"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i" rel="stylesheet">
</head>
<body ng-controller="mainCtrl as vm">

<table>
  <tr ng-repeat="x in valori">
    <td>{{ x.cardMoney }}</td>
    <td>{{ x.cashMoney }}</td>
    <td>{{ x.voucherNumber }}</td>
  </tr>
</table>

</body>
</html>

I have no idea why this doesn't work. Thank you in advance for taking the time to read this, and hopefully help me get through this issue :).

EDIT

I get this error: "SyntaxError: Unexpected token < in JSON at position 0" When I inspect the network tab, this is the response I get instead of the JSON.

<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'valori' in C:\wamp64\www\IncomeTracker\php\preluare.php on line <i>5</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0013</td><td bgcolor='#eeeeec' align='right'>240760</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\IncomeTracker\php\preluare.php' bgcolor='#eeeeec'>...\preluare.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0014</td><td bgcolor='#eeeeec' align='right'>242608</td><td bgcolor='#eeeeec'><a href='http://www.php.net/mysqli.mysqli' target='_new'>mysqli</a>
(  )</td><td title='C:\wamp64\www\IncomeTracker\php\preluare.php' bgcolor='#eeeeec'>...\preluare.php<b>:</b>5</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli::query(): Couldn't fetch mysqli in C:\wamp64\www\IncomeTracker\php\preluare.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0013</td><td bgcolor='#eeeeec' align='right'>240760</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\IncomeTracker\php\preluare.php' bgcolor='#eeeeec'>...\preluare.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0057</td><td bgcolor='#eeeeec' align='right'>243032</td><td bgcolor='#eeeeec'><a href='http://www.php.net/mysqli.query' target='_new'>query</a>
(  )</td><td title='C:\wamp64\www\IncomeTracker\php\preluare.php' bgcolor='#eeeeec'>...\preluare.php<b>:</b>7</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Call to a member function fetch_array() on null in C:\wamp64\www\IncomeTracker\php\preluare.php on line <i>10</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0013</td><td bgcolor='#eeeeec' align='right'>240760</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\IncomeTracker\php\preluare.php' bgcolor='#eeeeec'>...\preluare.php<b>:</b>0</td></tr>
</table></font>
share|improve this question
1  
whoa, don't build json like that. there's a method made specifically for that purpose. What html does it respond with? – Kevin B Aug 17 at 18:55
    
@KevinB I answered with a longer answer, see below :) – Paul Solomon Aug 17 at 19:07

It might be caused by that extra semicolon in the Content-Type header. Change the following line:

header("Content-Type: application/json;");

to:

header("Content-Type: application/json");
share|improve this answer
    
I changed it but sadly It's still the same. Thank you for your answer :) – Paul Solomon Aug 17 at 18:06
    
Try clearing the browser's cache. – ShiraNai7 Aug 17 at 18:12

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.