2017-09-17 58 views
0

我是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> 
+0

要獲得更有意義的回覆,請將完整的錯誤消息發佈爲好。 –

+0

@ jan.vdbergh「同樣沒有錯誤。」 – jdgregson

+0

@jdgregson首先,我使用了.then(function())。將它更改爲.catch後沒有錯誤。但仍然當我在服務器上運行它沒有打印。 –

回答

0

你需要使用catch得到錯誤信息,而不是then

$http.get("JSON.jsp").then(function(response){ 
    $scope.array=response.data; 

}).catch(function(error){ 
    console.log(error, 'can not get data.'); 
}); 
+0

謝謝薩奇拉。我改變了它。但是網頁上仍然沒有打印任何內容。似乎沒有任何內容存儲在'$ scope.array'對象內。 –

+0

您需要訪問'data'屬性。改變它'$ scope.array = response.data' –

+0

先生結果是相同的,即使它改爲'$ scope.array = response.data'後。你可以在你的機器上運行這個代碼並與我分享結果嗎? –

相關問題