【发布时间】:2015-04-08 23:42:00
【问题描述】:
我有一个与 CORS 相关的问题。我正在使用 Angular 和 Spring 编写客户端-服务器应用程序。当我想发送一些 HTTP 请求时,浏览器显示存在“跨源请求被阻止:同源策略不允许读取http://localhost:8080/...的远程资源”错误。我已经使用 Tomcat 在 localhost 上部署了 REST,现在我尝试使用 angular 使用任何响应,使用“npm”服务器部署,但由于 CORS 问题我不能这样做。我究竟做错了什么?这是代码片段:
服务器端:
@RequestMapping(value="/dummy", consumes="application/json", method=RequestMethod.GET)
public @ResponseBody String dummy() {
return "LOL";
}
客户端:
var authorizationApp = angular.module("authorizationApp", ['PageController','ngRoute']);
authorizationApp.config(function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
authorizationApp.config(['$routeProvider', function($routeProvider) {
.....
}])
var pageController = angular.module('PageController',[])
pageController.controller('PageCtrl',['$scope', '$routeParams', '$location', '$http', function($scope, $routeParams, $location, $http) {
..................................
$scope.someFunc = function() {
$http.get("http://localhost:8080/rabota/student/dummy")
.success(function(data, status,headers,config) {
alert("sent");
})
.error(function(data, status,headers,config) {
alert(":-(");
})
}
}])
【问题讨论】:
标签: java javascript angularjs spring