簡體   English   中英

如何從jsp返回json對象到angularjs ajax http.get?

[英]How to return json object from jsp to angularjs ajax http.get?

當我運行jsp文件時,我得到[{“ USRNM”:“ sahar”,“ USRPWD”:“ password”}]的信息是正確的,但是當我運行html時,我無法顯示數據,當我發出警報時,我將數據存儲在里面html標簽,而我嘗試使用以[object object]形式警告數據的json文件。請幫助我找出我做錯了什么。

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>

嘗試使用以下代碼,因為我認為從JSP返回的數據是字符串格式,因此在Angular JS中使用之前,需要將其轉換為JSON格式。

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
       $http.get("newjsp2.jsp").success(function (response) {
          $scope.names = JSON.parse(response);
      })
     .error(function(response,status,headers,config){
            alert('cannot find resource');
     }); 
  });

而不是使用response.getWritter ....我替換了3行,並刪除了jsp文件中的html標簽,所以現在它可以正常工作了:)

   // response.setContentType("json");
      // response.setCharacterEncoding("UTF-8");
   //response.getWriter().write(jsonArray.toString());

       //These 3 lines
       PrintWriter out1 = response.getWriter();
        out1.print(jsonArray);
        out1.flush();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM