anjular中的controller层$http服务,解决跨域请求。
js书写:
/**
* 采用CORS方式实现ajax跨域请求
*/
$http({
method:"post", // 请求方式可以为post,也可以为get
params:"",
url:"http://localhost:8080/getArray", // 跨域请求的路径
}).success(
function (response) {
$scope.array = response;
}
);
服务层书写:
@RequestMapping("/getArray")
@ResponseBody
public List<Integer> getArray(HttpServletResponse response) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(4);
// 此处要添加需要跨域时,响应头,"http://localhost" 为 前端页面请求的源地址
response.setHeader("Access-Control-Allow-Origin","http://localhost");
return list;
}
添加的响应头信息,可以通过 浏览器页面查看相应的请求origin;同时也可以看出响应的时候,服务端的响应信息是否添加成功
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。