解决前台ajax请求后台spring mvc 的跨域问题

特别鸣谢:https://blog.csdn.net/zjq852533445/article/details/79485870

          https://blog.csdn.net/qq767403510/article/details/54137160

话不多说直接上代码:

解决方法一:直接在@Controller层中的具体请求方法上添加@CrossOrigin即可;

例如:在请求方法上追击

方法二:在spring mvc 的spring-servlet.xml文件中添加对CORS(跨源资源共享)的支持,因为浏览器安全的基石是同源策略,在同源策略的限制下,非同源的网站之间不能发送 ajax 请求的,所以在spring的配置中配置CORS的支持即可

    <mvc:cors>
        <mvc:mapping path="/**" allowed-origins="*" allow-credentials="true" max-age="1800"
            allowed-methods="GET,POST,PUT,DELETE,PATCH,OPTIONS"/>
    </mvc:cors>

猜你喜欢

转载自blog.csdn.net/qq_39751996/article/details/85271844