1
當我運行jsp文件時,我得到[{「USRNM」:「sahar」,「USRPWD」:「password」}]這是正確的,但是當我運行html時,我不能顯示數據,當我提醒它時,數據在html標籤內,而我試着用[對象對象]的形式警告數據的json文件。請幫助我找出我做錯了什麼。如何將json對象從jsp返回到angularjs ajax http.get?
的jsp:
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select UsrNm,UsrPwd from dbo.tblUsr");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
JSONArray jsonArray = new JSONArray();
//List <JSONObject> jsonArray = new ArrayList<JSONObject>();
List <String> columnNames = new ArrayList<String>();
for(int i=1;i<=numberOfColumns;i++) {
columnNames.add(rsMetaData.getColumnName(i).toUpperCase());
}
while(rs.next()) {
JSONObject obj = new JSONObject();
for(int i=1;i<=numberOfColumns;i++) {
String key = columnNames.get(i - 1);
Object value = rs.getObject(i);
obj.put(key, value);
}
jsonArray.add(obj);
}
//response.setContentType("application/json");
// response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonArray.toString());
HTML:
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.USRNM}}</td>
<td>{{ x.USRPWD}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("newjsp2.jsp")
.success(function (response) {
// alert(JSON.stringify(response));
alert(response);
$scope.names = response;
}).error(function(response,status,headers,config){
alert('cannot find resource');
console.log(response);
console.log(status);
console.log(headers);
console.log(confiq);
});
});
</script>
什麼是alert(response);返回給你$ http.get(「newjsp2.jsp」)。success() –
它返回:[{「USRNM」:「sahar」,「USRPWD」:「密碼」}] <!DOCTYPE html>