简体   繁体   中英

$http.get unable to fetch data from SpringBoot controller

I am creating an application which will run queries on my store's database, based on what the user enters on the webpage. I have successfully created the backend method. And it successfully returns the response. But I am unable to retrieve the data and display it on my webpage in the form of a dynamic table. I am a bit new to AngularJS, so please bear with me, but any help is appreciated.

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;
}

I tried to model my controller in such a way that it can dynamically take the data received from the Java controller and assign it to the $scope variable.

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>

The table on the html page, works because I received it from this link http://jsfiddle.net/v6ruo7mj/1/

But it's not populating the tables with the data received from my backend controller method. I do not have any entities as this is just querying an existing database, so I need not to add any entities.

The issue probably is this line here in the service callback within your controller:

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

try with:

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

You already return the responses data in your service when calling:

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

Aside from your question I should mention that your Spring controller seems to be vunerable to SQL injection. It's in general not a good idea to allow the user to access your database directly. Although I don't know how your StoreService on the backend is implemented. But it seems as if an attacker could easily send a HTTP call to your endpoint and drop your database.

You have a typo in the runQuery function:

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
        });
    }
 }]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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