这是我的情况,我在 Netbeans 8.2 中有一个项目,在 html 中,这里调用 angularjs,但是当需要从 Angular 调用 java Controller 时,我们在 chrome 调试中收到“未找到”,在工厂中带有 $ http 发送“url”,但我不能调用 java Controller 。
-- 这是代码 html
<div data-ng-app="myApp">
<div data-ng-controller="MyController">
<button data-ng-click="getDataFromServer()">Test Angularjs</button>
</div>
</div>
-- 这是angularjs中的代码(这是一个File.js)
var app = angular
.module('myApp', ['ngRoute', 'ngResource'])
.controller('MyController', function ($scope, $service, $http, $resource) {
$scope.getDataFromServer = function () {
$service.JS_STST_GetData();
}
})// end controller
.factory(
'$service',
function ($http) {
return {
JS_STST_GetData: function (){
$http({
method: 'POST',
url: 'TestMaping',
headers: {
'Content-Type': 'application/json'
}
}).then(_success, _error);
},
-- 这是 Java Controller 代码
@Controller
public class VerController {
@RequestMapping(value="/TestMaping", method = RequestMethod.POST)
public String TEST(Model model){
return "Test";
}
@RequestMapping(method = RequestMethod.GET)
public String otroMetodo(Model model){
return "index";
}
}
最佳答案
您正在进行 POST
调用,而您的 Controller 请求映射的方法类型为 GET
,用于 url /TestMaping
。请更正并尝试。
关于java - 从 angularJs 调用 java Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46156252/