Perfect solution for cross-origin request errors in axios

Source: Internet
Author: User

Perfect solution for cross-origin request errors in axios

Error message:

Response to preflight request doesn't pass access control check: No 'access-Control-Allow-origin' header is present on the requested resource. origin 'HTTP: // localhost: 100' is therefore not allowed access. the response had HTTP status code 403

With the development of the front-end framework, data separation between the front-end and back-end has become a trend. That is to say, the front-end only needs to use ajax to request data synthesis pages at the backend. The back-end only provides data interfaces for data, the advantage is that front-end programmers no longer need to build their own Web servers, the front-end does not need to understand the back-end syntax, and the back-end does not need to understand the front-end syntax, which simplifies the development configuration, it also reduces the difficulty of cooperation.

The conventional GET, POST, PUT, and DELETE requests are simple requests (compared with the OPTIONS request), but the OPTIONS request is a bit special. It must first send a request to ask the server, do you want to request me, I want to send the data. (This is entirely based on my own understanding. If there is any error, please forgive me. Please refer to the original article by Dr. Ruan Yifeng .)

In the Vue project, the Http service uses Axios, and it uses the OPTIONS request.

If you only add 'access-Control-Allow-origin': * to the header, the problem cannot be solved. The error is shown at the beginning of the article.

Here we need to process the OPTIONS request in the background.

This document uses Spring MVC as an example:

Add an interceptor class:

public class ProcessInterceptor implements HandlerInterceptor {  @Override  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {    httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");    httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");    httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");    httpServletResponse.setHeader("X-Powered-By","Jetty");    String method= httpServletRequest.getMethod();    if (method.equals("OPTIONS")){      httpServletResponse.setStatus(200);      return false;    }    System.out.println(method);    return true;  }  @Override  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {  }  @Override  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {  }}

Configure Spring interceptor in spring-mvx.xml:

<mvc:interceptors>     <bean class="cn.tfzc.ssm.common.innterceptor.ProcessInterceptor"></bean>   </mvc:interceptors> 

Now, the problem has been solved:

The above Article perfectly solves the problem of cross-origin request errors in axios, that is, all the content I have shared with you. I hope to give you a reference and support for the customer's house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.