繁体   English   中英

$ http.get无法从SpringBoot控制器获取数据

[英]$http.get unable to fetch data from SpringBoot controller

我正在创建一个应用程序,该应用程序将根据用户在网页上输入的内容在商店的数据库上运行查询。 我已经成功创建了后端方法。 并成功返回响应。 但是我无法检索数据并将其以动态表格的形式显示在我的网页上。 我对AngularJS有点陌生,所以请多多包涵,但我们会提供任何帮助。

StoreController.java

@RequestMapping(value = "/runQuery", method = RequestMethod.GET)
public List<Map<String, Object>> runQuery(@RequestParam(value="query", defaultValue="* FROM items") String statement, Model model)  {
    List<Map<String, Object>> answer = storeService.executeUserQuery(statement);
    model.addAttribute("resultList", answer);
    return answer;
}

我试图对控制器进行建模,使其可以动态地获取从Java控制器接收到的数据并将其分配给$ scope变量。

app.module.js

(function(){
    'use strict';

    angular.module('app', []);
})();

store.controller.js

angular
.module('app').controller('StoreController', ['$scope','StoreService','StoreController','$q', function ($scope,StoreService, StoreController, $q) {

    $scope.runQuery = function () {

               StoreService.runQuery($scope.statement)
              .then (function (data){
                  $scope.rows = response.data;
                  $scope.cols = Object.keys($scope.rows[0]);
              },
              function error(response){
                  if (response.status == 404){
                  $scope.errorMessage = response.data[0];
              }
                  else {
                      $scope.errorMessage = 'Error displaying result user!';
                  }
            });
          }
    }

]);

app.service('StoreService',['$http', function ($http,$q) {

    this.runQuery = function runQuery(statement){
        return $http({
          method: 'GET',
          url: 'http://localhost:8080/runQuery/',
          params: {statement:statement},
          headers: 'Accept:application/json'
        }).then( function(response){
            return reponse.data;
        });
    }

index.html

<body data-ng-app="app" data-ng-controller="StoreController">
    <div class="container">

        <form th:action="@{/logout}" method="get">
            <button class="btn btn-md btn-danger btn-block"
                style="color: #fff; background-color: #e213a2; border-color: #c3c2c0;"
                name="registration" type="Submit">Logout</button>
        </form>

        <div class="panel-group" style="margin-top: 40px">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <span th:utext="${userName}"></span>
                </div>
                    <div>
                <form name="queryForm" method="get" data-ng-submit="runQuery()">
                    <div class="panel-body">
                        <h3 id="queryLabel">Select Query:</h3>
                        <textarea id="query" wrap="soft"
                            placeholder="Please do not enter SELECT with your query, it's added automatically!!" data-ng-model="statement"></textarea>
                        <button type="submit">Run Query</button>
                    </div>
                    </form>
                    <div class="panel-body" id="results">
                        <h3 id="queryLabel">Result:</h3>
                        <table border="1">
                            <tr>
                                <th data-ng-repeat="column in cols">{{column}}</th>
                            </tr>
                            <tr data-ng-repeat="row in rows">
                                <td data-ng-repeat="column in cols">{{row[column]}}</td>
                            </tr>
                        </table>
                    </div>

                </div>
                <div>
                    <p class="admin-message-text text-center" th:utext="${adminMessage}"></p>

                </div>
    </div>
    </div>
    </div>
</body>

html页面上的表有效,因为我是从此链接http://jsfiddle.net/v6ruo7mj/1/收到的

但这不是用从我的后端控制器方法接收的数据填充表。 我没有任何实体,因为这只是查询现有数据库,因此不需要添加任何实体。

问题可能是控制器中的服务回调中的以下行:

.then (function (data){
    $scope.rows = response.data;
    // ...
}

尝试:

.then (function (data){
    $scope.rows = data;
    // ...
}

调用时,您已经在服务中返回了响应数据:

}).then( function(response){
        return reponse.data;
    });

除了您的问题外,我还应该提到您的Spring控制器似乎很容易受到SQL注入的攻击。 通常,不允许用户直接访问您的数据库不是一个好主意。 尽管我不知道后端的StoreService如何实现。 但是,似乎攻击者可以轻松地将HTTP调用发送到您的端点并删除数据库。

您在runQuery函数中有一个错字:

app.service('StoreService',['$http', function ($http,$q) {

    this.runQuery = function runQuery(statement){
        return $http({
          method: 'GET',
          url: 'http://localhost:8080/runQuery/',
          params: {statement:statement},
          headers: 'Accept:application/json'
        }).then( function(response){
            ̶r̶e̶t̶u̶r̶n̶ ̶ ̶r̶e̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶;̶
            return response.data
        });
    }
 }]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM