【发布时间】:2018-02-19 17:28:13
【问题描述】:
这是我的情况,我在 Netbeans 8.2 中有一个项目,在 html 中,这里调用 angularjs,但是当需要从 angular 调用 java 控制器时,我们在 chrome 调试中收到“未找到”,在工厂中使用 $ http发送“url”,但我不能调用java控制器。
-- 这是代码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
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";
}
}
【问题讨论】:
-
您正在向仅接受 GET 请求的控制器发送 POST 请求。这可能是问题所在。
-
你的代码在我的机器上运行,
GET请求从文件中提取数据。我希望你已经定义了_sucess和_error函数。假设你的 JS 代码一切正常,我希望你在开头添加一个斜杠url: '/TestMaping',
标签: java angularjs model-view-controller controller