对角度来说非常新 - 目前只是在探索。非常感谢您提供一两个关于我将在哪里拧紧的指针 - TIA
我想做的是从 mysql 选择查询中获取数据并将其返回到 html 页面,使用 json 然后有角度来显示它。已成功使用 JSON_encode 返回数据 - 通过 jsonlint 运行它并且它已经恢复正常。为了测试,然后我使用了这个字符串并创建了一个文本文件,并通过 angular 文件运行它——它工作正常。
如果我直接从 php 文件调用数据,它会失败,但如果我从静态文本文件调用数据(看起来与 php 数据的输出相同),它就可以工作。
我正在发送带有回声的数据 - 这是正确的吗?
$json=json_encode($main_arr);
echo $json;
json输出:
[{"custcode":"CMZIG001","cli":"0020\/1"},{"custcode":"CMZIG002","cli":"0020\/2"},{"custcode":"CMZIG003","cli":"0020\/3"},{"custcode":"999","cli":"002871365801"},{"custcode":"CMSLE001","cli":"0030"},{"custcode":"CMNIC001","cli":"0034"},{"custcode":"CMLIF001","cli":"0047"},{"custcode":"CMTON01101","cli":"0087\/1"},{"custcode":"CMTON01102","cli":"0087\/2"},{"custcode":"CMTRE001","cli":"0090"}]
文本文件内容:
[{"custcode":"CMZIG001","cli":"0020\/1"},{"custcode":"CMZIG002","cli":"0020\/2"},{"custcode":"CMZIG003","cli":"0020\/3"},{"custcode":"999","cli":"002871365801"},{"custcode":"CMSLE001","cli":"0030"},{"custcode":"CMNIC001","cli":"0034"},{"custcode":"CMLIF001","cli":"0047"},{"custcode":"CMTON01101","cli":"0087\/1"},{"custcode":"CMTON01102","cli":"0087\/2"},{"custcode":"CMTRE001","cli":"0090"}]
回应评论:
好的 - 我希望我的代码不会让你感到厌烦。一个html文件如下(我会尝试正确格式化)
<!doctype html>
<html ng-app="App">
<head>
<meta charset="utf-8">
<title>CLIs http</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js">
</script>
<script src="app.js"></script>
</head>
<body ng-controller="TodoCtrl">
<ul>
<li ng-repeat="cli in cli">
{{cli.custcode}} - <em>{{cli.cli}}</em>
</li>
</ul>
</body>
</html>
app.js - 当前设置为在我的服务器上调用一个 php 文件
var App = angular.module('App', []);
App.controller('TodoCtrl', function($scope, $http) {
$http.get('http://localhost/t5/clis.json')
//$http.get('njs.json')
.then(function(res){
$scope.cli = res.data;
});
});
clis.json(我服务器上的 php 文件)
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db='mutuxf';
mysql_select_db($db, $con);
$result = mysql_query("SELECT custcode, cli FROM clis limit 10");
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $value)
{ $arr[$key] = $value; }
$main_arr[] = $arr;
}
$json=json_encode($main_arr);
echo $json;
?>
当我使用 php 文件来提高数据时,我只得到一个没有文本的项目符号列表,但是当我使用文本文件 (njs.json) 时,页面可以正常工作。