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.


这个是提示服务端没有设置 '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值,完成后重启服务,

即可正常访问!



猜你喜欢

转载自blog.csdn.net/vtopqx/article/details/80747258