<script>
angular.module("suiteESCWeb", ["ngMessages"]);
angular.module("suiteESCWeb").controller("usuarioCtrl", function ($scope, $http) {
$scope.app = "Lista Telefonica";
$scope.usuario = [];
var config = {headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET, POST, PUT, DELETE',
'X-Requested-With': 'XMLHttpRequest',
'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept'
}
};
var carregarUsuario = function () {
$http.get("http://localhost:8080/usuario/1", config).success(function (data) {
console.log(data);
$scope.usuario = data;
}).error(function (data, status) {
$scope.message = "Aconteceu um problema: " + data;
});
};
carregarUsuario();
});
</script>
我开始研究AngularJS,Back-End的一部分正在完美地完成由邮递员在chrome上测试的URL现在尝试集成,尝试进行这个简单的调用并且似乎永远不会传递标题。
我一直有的错误是:
{"timestamp":1473905412331,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'null' not supported","path":"/usuario/1"}
public class CORSFilter 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, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
答案 0 :(得分:0)
试试这个,
$http({
method: "GET",
url: 'http://localhost:8080/usuario/1',
headers: config
}).
success(function (data, status, headers, config) {
}).
error(function (data, status, headers, config) {
});