gpt4 book ai didi

javascript - localhost 不工作的简单 angularJS GET 函数

转载 作者:数据小太阳 更新时间:2023-10-29 05:25:54 26 4
gpt4 key购买 nike

我有一个非常简单的 Spring Rest 后端,它返回 JSON 数据。 Restful URL 在我的浏览器中是这样工作的:

http://localhost:8080/abc/runlist

它返回的数据是这样的:

[
{"stock_num":"KOH19","damage":"Toronto (Oshawa)"},
{"stock_num":"AZ235","damage":"Toronto (Oshawa)"},
...
]

我有一个独立的 html 页面,它不是我的网络应用程序的一部分。我只是想测试一下我的 Angular 代码是否正在获取数据然后循环遍历它。

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl">

<ul>
<li ng-repeat="x in names">
{{ x }}
</li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost:8080/abc/runlist")
.success(function (response) {$scope.names = response.records;});
});
</script>

yo yo

</body>
</html>

为什么它不起作用?我在 Spring 中遇到了一些东西,你需要实现一个叫做 CORS 的东西。我这样做了,但仍然没有任何返回:

@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain
chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS,
DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}

最佳答案

尝试这样的事情:

js:

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost:8080/abc/runlist")
.success(function (response){
$scope.names = records;
});
});

html:

<ul>
<li ng-repeat="x in records">
{{x}}
</li>
</ul>

更多参数 html:

<ul>
<li ng-repeat="x in records">
{{x.myName}}
{{x.myNumber}}
</li>
</ul>

希望我对您有所帮助。

关于javascript - localhost 不工作的简单 angularJS GET 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30464820/

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