基本上我要做的是从我的数据库中删除重复记录,但是,我收到以下错误'SyntaxError:位于0的JSON中的意外标记F'。
这是我的代码:
HTML
<tr ng-repeat="x in data">
<td>{{x.songName}}</td>
<td>{{x.albumName}}</td>
</tr>
JS
$scope.loadTracks = function(){
$http.get('includes/functions/gettrack.php').success(function(data){
$scope.data = data;
})
}
PHP
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$data = array();
$sql = "SELECT * FROM tracks";
// Create a prepared statement
$stmt = mysqli_stmt_init($connection);
// Prepare the prepared statement
if (!mysqli_stmt_prepare($stmt, $sql)) {
// Error exists
die('Fatal error: service unavailable at this time');
} else {
// Run parameters inside the database
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) > 0) {
// Check for results and assign results to array
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
echo json_encode($data);
}
}
?>
答案 0 :(得分:2)
我怀疑您的代码正在执行die('Fatal error: service unavailable at this time');
。该字符串的字符0是F
,与您的错误相匹配。
JS将尝试解码无效JSON的Fatal error: service unavailable at this time
。
die()未设置http代码,因此AJAX请求将被视为成功。尝试使用http_response_code()设置代码:
if (!mysqli_stmt_prepare($stmt, $sql)) {
// Error exists
http_response_code(500);
die('Fatal error: service unavailable at this time');
} else {
您还应调整AJAX调用以处理错误:
$http.get('includes/functions/gettrack.php').success(function(data){
$scope.data = data;
}).fail(function(){
console.error('AJAX call failed');
});
或者你可以更改die()内返回的字符串,但我会建议这不是解决它的好方法。
die('"Fatal error: service unavailable at this time"');
答案 1 :(得分:0)
您错过了与数据库的连接......
$connect = mysqli_connect($db['hostname'],$db['username'],$db['password'],$db['database']);