[英]Can't get JSON data from 000webhost server with AngularJS $http.get
I am trying to get data from my server in a HTML file. 我正在尝试从服务器以HTML文件形式获取数据。
I tried w3 schools code but when I try to use my server link it prints nothing. 我尝试了w3学校代码,但是当我尝试使用服务器链接时,它什么也不会打印。
Here is the angular code: 这是角度代码:
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http
.get("http://rabikhan.net23.net/Bitm_Student_Project/src/test.php")
.then(function (response) {$scope.names = response.data.records;});
});
</script>
and the php code 和PHP代码
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("mysql4.000webhost.com", "a1724083_rhk", "r7224191", "a1724083_tour2");
$result = $conn->query("SELECT CompanyName, City, Country FROM customers");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["CompanyName"] . '",';
$outp .= '"City":"' . $rs["City"] . '",';
$outp .= '"Country":"'. $rs["Country"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
Question 题
How can I have echo($outp);
我怎么有
echo($outp);
print the desired information? 打印所需的信息?
change your php to: 将您的PHP更改为:
<?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8"); $conn = new mysqli("mysql4.000webhost.com", "a1724083_rhk", "r7224191", "a1724083_tour2"); $result = $conn->query("SELECT CompanyName AS Name, City, Country FROM customers"); // create an empty output array $output = array(); while ($rs = $result->fetch_array(MYSQLI_ASSOC)) { // add record to output array $output.push($rs); } $conn->close(); // return output array as proper json echo json_encode($output); ?>
I also noticed that you have some analytics code in your php response pointing to http://stats.hosting24.com/count.php , make sure not to append it to your JSON responses 我还注意到,您的php响应中有一些分析代码指向http://stats.hosting24.com/count.php ,请确保不要将其附加到JSON响应中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.