2017-09-17 130 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'后。你可以在你的机器上运行这个代码并与我分享结果吗? –

相关问题