Ajax跨域访问

来源:互联网 发布:电脑windows无法启动32 编辑:程序博客网 时间:2024/06/01 09:43

最近,学了springMVC,SpringBoot(两个都可以做web服务,收发http请求),心血来潮,想用Ajax访问这个服务

不仅要在项目中使用ajax,,还要开两个服务器,,用ajax去访问,实现,任何地点的访问(哈哈说的有点大)


但是ajax 访问其他服务器会出现跨域问题,在这里直接给出,博主的解决方案:

错误代码:

XMLHttpRequest cannot load http://localhost:8080/hello/get_users_json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.



解决方法:

响应头中加入:(据说可能造成安全问题)

respons.setHeader("Access-Control-Allow-Origin", "*");
原创粉丝点击