Home > Web Front-end > JS Tutorial > Use SpringMVC to solve vue cross-domain requests

Use SpringMVC to solve vue cross-domain requests

亚连
Release: 2018-06-06 14:08:24
Original
2754 people have browsed it

Below I will share with you an article using SpringMVC filters to solve the problem of vue cross-domain requests. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

I have written before about how to solve cross-domain requests through annotations. You need to use annotations in the control class every time. This time, we solved it through the interceptor of springmvc:

The class HandlerInterceptor that inherits SpringMVC overrides the preHandle method. This method will be called before reaching controll, as follows

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
        Object handler) throws Exception { 
  response.setHeader("Access-Control-Allow-Origin", "*"); 
  response.setHeader("Access-Control-Allow-Methods", "*"); 
  response.setHeader("Access-Control-Max-Age", "3600"); 
  response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
  response.setHeader("Access-Control-Allow-Credentials","true"); //是否允许浏览器携带用户身份信息(cookie) 
  return true; 
 }
Copy after login

springmvc configuration is as follows:

<mvc:interceptors > 
 <!--过滤所有请求,处理跨域请求问题--> 
  <mvc:interceptor> 
   <mvc:mapping path="/**"/> 
   <bean class="com.jzy.interceptor.CommonInterceptor"></bean> 
  </mvc:interceptor> 
</mvc:interceptors >
Copy after login

This can solve the limitation of cross-domain requests when SSM VUE is separated from the front and back ends.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

About explaining in detail the use of take in redux-saga

How to implement breakpoints in Visual Studio Code Debugging Vue

How to add, delete and modify query methods through tables in angularJs

The above is the detailed content of Use SpringMVC to solve vue cross-domain requests. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template