【发布时间】:2018-01-30 14:35:07
【问题描述】:
我想在 angularjs 中从 servlet 返回我的 json 对象,我编写了一个 Angular 应用程序,它没有给出任何错误,但也没有获取数据。我无法正确搜索,因为我的英语有些不足,请有人帮助我 这是我的代码
Angularjs
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="x in myData">
{{ x.1}}
</li>
</ul>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
$http.get("first").then(function (res) {
$scope.myData = res.data.jso;
});
});</script>>
这里是我的 doGet 方法,我的 json 对象在里面;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Connection conn = DriverManager.getConnection(url, uname, pass);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
JSONObject jso = new JSONObject();
jso.put("1", rs.getString(1));
jso.put("2", rs.getString(2));
jso.put("3", rs.getString(3));
jso.put("4", rs.getString(4));
jso.put("6", rs.getString(6));
jso.put("8", rs.getString(8));
pw.print(jso);
}
conn.close();
rs.close();
pw.close();
stmt.close();
pw.flush();
} catch (Exception e) {
out.println(e.getMessage());
}
}
【问题讨论】:
-
您尝试使用 x["1"] 的出租车吗?
-
使用
console.log($scope.myData);看看会打印什么 -
我都试过了,但没有成功没有错误没有数据
标签: java angularjs json servlets