我在AngularJS和Spring MVC中使用ajax调用。请帮我解决问题。谢谢。
Spring-MVC控制器
@Controller
public class MasterController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public @ResponseBody String helloMsg() {
return "Hello";
}
}
Spring MVC mapping
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:default-servlet-handler/>
<mvc:resources location="/static/**" mapping="/static/"/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.mvc.ng.controller"/>
的index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to home page</title>
<script src="./static/lib/angular.js"></script>
<script src="./static/lib/jquery.js"></script>
<script src="./static/js/controller.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="myController">
<h2>{{name}}</h2>
<h2>{{message}}</h2>
<h2>{{working}}</h2>
<input ng-click="hello()" type="button" value="ajax">
</div>
</div>
</body>
</html>
Angular App and Controller:
var myApp=angular.module("myApp",[]);
myApp.controller("myController",["$scope",function($scope){
$scope.name="XYZ";
$scope.message="Hello";
$scope.hello=function(){
$.ajax({
type:'GET',
dataType:'text',
url:'/MVC-NG-Integration/hello',
success:function(data){
console.log('working');
},
error:function(error)
{
console.log(error);
}
});
}
}]);
一切似乎都很好但是当我打电话给ajax时它会返回 - &#34;无法加载资源:服务器响应状态为404(未找到)&#34; &#34; http://localhost:2016/MVC-NG-Integration/hello&#34; 我哪里出错?我还没有配置viewResolver,因为我不会使用任何JSP。 我甚至使用了$ http但没有成功。