2017-09-17 4 views
0

AngularJSのJSONと$ httpサービスの新機能です。私は、同じフォルダにあるjspファイルから応答を取得しようとしています。しかし、私はそれをサーバー上で実行すると何も印刷されません。また、エラーはありません。

誰か助けてください。この件に関してお時間をいただきありがとうございました。

以下

は私がAngularJSを使用してきた私のhtmlファイルです:

以下
<!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

あなたの代わりにthen

のエラーメッセージを取得するために catchを使用する必要があります
$http.get("JSON.jsp").then(function(response){ 
    $scope.array=response.data; 

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

ありがとうSachila。私はそれをキャッチするように変更しました。しかし、まだ何もウェブページには印刷されていません。 '$ scope.array'オブジェクトの中には何も格納されていないようです。 –

+0

'data'プロパティにアクセスする必要があります。 $ scope.array = response.data' –

+0

それを '$ scope.array = response.data'に変更しても同じ結果になります。あなたはあなたのマシンでこのコードを実行して結果を教えてください。 –

関連する問題