【发布时间】:2021-05-23 14:39:00
【问题描述】:
我有一个简单的网络服务:
@SpringBootApplication
@RestController
@RequestMapping(value="hello")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RequestMapping(value="/{firstName}/{lastName}",method = RequestMethod.GET, produces = "application/json")
public String hello( @PathVariable("firstName") String firstName,
@PathVariable("lastName") String lastName) {
return String.format("{\"message\":\"Hello %s %s\"}", firstName, lastName);
}
}
我在 8081 端口运行,我可以从浏览器获取它。
但是,从角度来看,我得到错误,响应状态 -1:
<div ng-app="myApp" ng-controller="myCtrl">
<p>Today's welcome message is:</p>
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("http://localhost:8081/hello/test/gfgg")
.then(function success(response) {
$scope.myWelcome = "response.data";
},
function error(response) { // Response status code 4xx and 5xx
console.log("error");
$scope.myWelcome = response.status + ": " + JSON.stringify(response);
});
});
</script>
</body>
</html>
当我使用一些公共服务时,例如 http://md5.jsontest.com/?text=1234,角度工作正常。
有人知道如何让本地服务以 Angular 工作吗?
【问题讨论】:
-
-1 的状态通常会发生 CORS 错误。您应该会在开发者控制台中看到该消息。
标签: angularjs cors same-origin-policy