我是AngularJS中的JSON和$ http服務的新手。我試圖從一個jsp文件(它在同一個文件夾中)得到響應。但是當我在服務器上運行它時,什麼都不打印。也沒有錯誤。
有人請幫忙。非常感謝您在這個問題上的時間和協助。
下面是我的html文件,其中我用AngularJS:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.js"> </script>
<script>
var myApp=angular.module("myApp",[]);
myApp.controller("myController",function($scope,$http){
$http.get("JSON.jsp").then(function(response){
$scope.array=response;
}).then(function(error){
console.log(error, 'can not get data.');
});
});
</script>
<meta charset="ISO-8859-1">
<title>AngularCheck</title>
</head>
<body ng-controller="myController" >
<div ng-repeat="arr in array">
{{arr.name}}
{{arr.age}}
</div>
</body>
</html>
下面是我的JSP文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSON page</title>
</head>
<body>
<%
out.println(" [{\"name\":\"jothi\",\"age\":\"20\"},{\"name\":\"Guru\",\"age\":\"22\"}]");
%>
</body>
</html>
要獲得更有意義的回覆,請將完整的錯誤消息發佈爲好。 –
@ jan.vdbergh「同樣沒有錯誤。」 – jdgregson
@jdgregson首先,我使用了.then(function())。將它更改爲.catch後沒有錯誤。但仍然當我在服務器上運行它沒有打印。 –