angularjs - HTTP 状态 405 – 不允许的方法 不支持请求方法“GET”
问题描述
我正在关注使用 spring、angular js、Spring REST 服务的电子商务网站的教程。当我尝试使用“MovieCartResources”类中的 RESTful 服务时,我收到错误 HTTP 状态 405 – 方法不允许。这是我在这里的第一篇文章,如果我犯了一些错误,我很抱歉。
MovieCartResources 类
@Controller
@RequestMapping("/rest/cart")
public class MovieCartResources {
@Autowired
private MovieCartService movieCartService;
@Autowired
private CustomerService customerService;
@Autowired
private MovieService movieService;
@RequestMapping("/{cartId}")
public @ResponseBody
MovieCart getMovieCart(@PathVariable(value = "cartId") int cartId) {
return movieCartService.getMovieCartById(cartId);
}
@RequestMapping(value = "/add/{movieId}", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addItem(@PathVariable("movieId") int movieId,
@AuthenticationPrincipal User activeUser) {
Customer customer =
customerService.getCustomerByUsername(activeUser.getUsername());
MovieCart movieCart = customer.getMovieCart();
Movie movie = movieService.getMovieById(movieId);
List<MovieItem> movieItemList = movieCart.getMovieItem();
for (int i = 0; i < movieItemList.size(); i++) {
if (movieItemList.get(i).getMovie().getMovieId() ==
movie.getMovieId()) {
MovieItem movieItem = movieItemList.get(i);
movieItem.setQuantity(movieItem.getQuantity() + 1);
movieCartService.addMovieCartItem(movieItem);
return;
}
}
MovieItem movieItem = new MovieItem();
movieItem.setQuantity(1);
movieItem.setMovie(movie);
movieItem.setMovieCart(movieCart);
movieCartService.addMovieCartItem(movieItem);
}
角度js文件
var cartApp = angular.module("cartApp", []);
cartApp.controller("cartCtrl", function ($scope, $http) {
$scope.refreshCart = function () {
$http.get('/eShop/rest/cart/' + $scope.movieCartId).success(function (data) {
$scope.movieCart = data;
});
};
$scope.clearCart = function () {
$http.delete('/eShop/rest/cart/' +
$scope.movieCartId).success($scope.refreshCart());
};
$scope.initCart = function (movieCartId) {
$scope.movieCartId = movieCartId;
$scope.refreshCart();
}
$scope.putToCart = function (movieId) {
$http.put('/eShop/rest/cart/add/' + movieId).success(function () {
alert("Movie successfully added to movie list!")
});
};
$scope.removeFromCart = function(movieId){
$http.put('/eShop/rest/cart/remove/' + movieId).success(function(){
$scope.refreshCart();
})
}
});
电影购物车.jsp 文件
<%@include file="/WEB-INF/views/template/header.jsp" %>
<div class="container-wrapper">
<div class="container">
<div class="jumbotron">
<h1>Movie list</h1>
<p>Here is your movie list</p>
</div>
<br>
<section class="container" ng-app="cartApp">
<div ng-controller="cartCtrl">
<div>
<a class="btn btn-danger -pull-left" ng-
click="clearCart()">
<span class="glyphicon glyphicon-remove-sign"></span>
Clear cart
</a>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>IMDB</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="movieItem in movieCart.movieItem">
<td>{{movieItem.movie.movieName}}</td>
<td>{{movieItem.movie.movieType}}</td>
<td>{{movieItem.movie.movieIMDB}}</td>
<td>{{movieItem.movie.movieDuration}}</td>
<td><a href="#" class="label label-danger" ng-
click="removeFromCart(movieItem.movie.movieId)">
<span class="glyphicon glypicon-remove"></span>
remove
</a></td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
</div>
<script src="<c:url value="/resources/vendor/bootstrap/js/controller.js" /> "></script>
<%@include file="/WEB-INF/views/template/footer.jsp" %>
当我尝试在 controller.js 类中使用我的 PUT、DELETE、GET 方法时,我得到 HTTP 状态 500 – 内部服务器错误或 HTTP 状态 405 – 方法不允许。我是 Angular 的新手,不知道如何解决这个错误。在我的“header.jsp”文件中,我有 Angular js 和 Jquery 的脚本。感谢您的任何帮助。
解决方案
推荐阅读
- c# - 反序列化 XML 时绕过特定的父节点?
- reactjs - 如何在 MobX 中使用 @observable 装饰器定义的更改对象上添加侦听器以在 React 中使用
- javascript - 运行 minio 服务器时不会出现对 minio 浏览器的更改
- javascript - 关闭 ASP.NET 页面
- r - 循环中对数据帧的操作
- computer-vision - 转换 TensorFlow 模型时出现 OpenVino 模型优化器错误
- sql-server - 如何基于Select查询在sql server中添加新列
- python - 有没有办法在 FacetGrid seaborn 中指定子图标题?
- akeneo - Akeneo 导入在上传时返回 500
- sql - SQL Server 查询以查找没有关联的透视表之间的记录