gpt4 book ai didi

javascript - $http.get无法从SpringBoot Controller 获取数据

转载 作者:行者123 更新时间:2023-11-28 17:46:32 25 4
gpt4 key购买 nike

我正在创建一个应用程序,它将根据用户在网页上输入的内容对我的商店数据库运行查询。我已经成功创建了后端方法。并且成功返回响应。但我无法检索数据并将其以动态表格的形式显示在我的网页上。我对 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;
}

我尝试以这样的方式对我的 Controller 进行建模,使其可以动态获取从 Java Controller 接收的数据并将其分配给 $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/

但它没有使用从我的后端 Controller 方法接收到的数据填充表。我没有任何实体,因为这只是查询现有数据库,所以我不需要添加任何实体。

最佳答案

问题可能出在 Controller 内的服务回调中的这一行:

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

尝试:

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

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

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

除了你的问题之外,我还应该提到你的 Spring Controller 似乎很容易受到 SQL 注入(inject)的攻击。一般来说,允许用户直接访问您的数据库并不是一个好主意。虽然不知道你后端的StoreService是怎么实现的。但攻击者似乎可以轻松地向您的端点发送 HTTP 调用并删除您的数据库。

关于javascript - $http.get无法从SpringBoot Controller 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com