Ajax跨域访问抱错 原因:CORS 头缺少 ‘Access-Control-Allow-Origin‘

6 篇文章 0 订阅

当Ajax跨域访问,明明已经获取到访问的json数据但是浏览器报错CORS 头缺少 ‘Access-Control-Allow-Origin’ ;
解决方式一:
过滤器

package com.xet.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

public class CrosFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("进入filter");
		HttpServletResponse res = (HttpServletResponse)response;
		HttpServletRequest req = (HttpServletRequest) request;
		String origin = req.getHeader("Origin");
		if(!StringUtils.isEmpty(origin) ) {
			res.addHeader("Access-Control-Allow-Origin", origin);
		}
		
		res.addHeader("Access-Control-Allow-Methods", "*");	
		//post请求 带Json参数
		res.addHeader("Access-Control-Allow-Headers","content-type");
		//在指定时间内 缓存预检命令
		res.addHeader("Access-Control-Max-Age","3600");
		chain.doFilter(request, response);
	}
}
2.在web.xml中注册该过滤器

<!-- 过滤器配置 -->
  <filter>
     <display-name>ajax过滤器</display-name>
     <filter-name>CrosFilter</filter-name>
     <filter-class>com.xet.filter.CrosFilter</filter-class>
  </filter>
  
  <filter-mapping>
     <filter-name>CrosFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

或者springboot注册过滤器

package com.xet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.xet.controller.CrosFilter;

@SpringBootApplication
public class AjaxServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(AjaxServerApplication.class, args);
	}

	@Bean
	public FilterRegistrationBean registerFiler() {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		bean.addUrlPatterns("/*");
		bean.setFilter(new CrosFilter());
		return bean;
	}
}

第二种利用Spring框架注解@CrossOrigin
在ajax请求的类或者方法上加@CrossOrigin注解

package com.xet.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
@CrossOrigin
public class TestController {
	
	@GetMapping("/get1")
	private ResultBean get1(HttpServletResponse res) {
		System.out.println("TestController get1()");
		return new ResultBean(200, "get1 ok");
	}
	
	@PostMapping("/postJson")
	private ResultBean postJson(@RequestBody User user) {
		System.out.println("TestController.postJson");
		return new ResultBean(200, "postJson"+user.getName());
	}
}{
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值