加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax请求Access-Control-Allow-Origin错误解决

发布时间:2020-12-16 03:10:29 所属栏目:百科 来源:网络整理
导读:在JS中使用AJAX请求跨域时候,会提示错误 Failed to load http://localhost:8889/xxxxxxxxx: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8085' is therefore not allowed access. 这个是提示

在JS中使用AJAX请求跨域时候,会提示错误

Failed to load http://localhost:8889/xxxxxxxxx: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8085' is therefore not allowed access.


这个是提示服务端没有设置'Access-Control-Allow-Origin'

以Spring mvc为例

设置Access-Control-Allow-Origin即可,如下:

@RequestMapping(value = "/test",method = RequestMethod.POST)
@ResponseBody
public String test(String param,HttpServletResponse response) {
	response.setHeader("Access-Control-Allow-Origin","*");
	return "test";
}

设置Header值,完成后重启服务,

即可正常访问!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读