AngularJS - $ http.get返回JSON的HTML内容(php& mysql)

时间:2016-08-17 18:02:10

标签: javascript php mysql angularjs json

我正在尝试使用php从mysql数据库中获取一些数据,并将其显示在angular内。我正在使用WAMP服务器在本地托管所有内容。

然而,我得到的回应是HTML内容为JSON。

以下是我的代码段:

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>

我不知道为什么这不起作用。提前感谢您花时间阅读本文,希望能帮助我解决这个问题:)。

修改

我收到此错误:“SyntaxError:位于0的JSON中的意外标记&lt;” 当我检查网络选项卡时,这是我得到的响应,而不是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>

2 个答案:

答案 0 :(得分:0)

显然,这是一个线索:

警告:mysqli :: mysqli():( HY000 / 1049):未知数据库&#39; valori&#39;在C:\ wamp64 \ www \ IncomeTracker \ php \ preluare.php

答案 1 :(得分:-2)

可能是由Content-Type标头中的额外分号引起的。更改以下行:

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

为:

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