Angular $http.get 与我预期的 return 不符
Angular $http.get doesn't return what i expected
我正在尝试执行一个简单的 http get 请求,但它没有return我的预期。
这是代码
.js :
$http.get("database.php").success(function(data, status)
{
alert(data);
$scope.data = data;
}
).error(function(data, status)
{
$scope.untruc = "Error";
});
.php :
<?php
header('Content-type: application/json');
$conn = // my connection
// Check connection
if ($conn->connect_error)
echo "Connection failed";
else
{
$sql = "SELECT id FROM user";
$result = $conn->query($sql);
echo json_encode($result);
}
?>
输出为:
data : <?php header('Content-type: application/json'); $conn = // my connection; // Check connection if ($conn->connect_error) echo "Connection failed"; else { $sql = "SELECT id FROM user"; $result = $conn->query($sql); echo json_encode($result); } ?>
字面上的代码...
我期待它获得 $result。你知道我该怎么做吗?
请检查您的 php 代码一次。
并尝试实现此代码
<?php
header('Content-type: application/json');
$conn = new mysqli('localhost', 'root', 'password', 'database');
// Check connection
if ($conn->connect_error)
echo "Connection failed";
else
{
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
$myresult = [];
$i =0;
while($row = $result->fetch_array()) {
$myresult[$i] = $row;
$i++;
}
//echo "<pre>";
echo json_encode($myresult);
}
?>
我正在尝试执行一个简单的 http get 请求,但它没有return我的预期。
这是代码
.js :
$http.get("database.php").success(function(data, status)
{
alert(data);
$scope.data = data;
}
).error(function(data, status)
{
$scope.untruc = "Error";
});
.php :
<?php
header('Content-type: application/json');
$conn = // my connection
// Check connection
if ($conn->connect_error)
echo "Connection failed";
else
{
$sql = "SELECT id FROM user";
$result = $conn->query($sql);
echo json_encode($result);
}
?>
输出为:
data : <?php header('Content-type: application/json'); $conn = // my connection; // Check connection if ($conn->connect_error) echo "Connection failed"; else { $sql = "SELECT id FROM user"; $result = $conn->query($sql); echo json_encode($result); } ?>
字面上的代码...
我期待它获得 $result。你知道我该怎么做吗?
请检查您的 php 代码一次。 并尝试实现此代码
<?php
header('Content-type: application/json');
$conn = new mysqli('localhost', 'root', 'password', 'database');
// Check connection
if ($conn->connect_error)
echo "Connection failed";
else
{
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
$myresult = [];
$i =0;
while($row = $result->fetch_array()) {
$myresult[$i] = $row;
$i++;
}
//echo "<pre>";
echo json_encode($myresult);
}
?>