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

前端之家收集整理的这篇文章主要介绍了Ajax请求Access-Control-Allow-Origin错误解决前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在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.


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

@H_403_18@以Spring mvc为例

@H_403_18@设置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值,完成后重启服务,

即可正常访问!

猜你在找的Ajax相关文章