Cross-domain issues in front-end and back-end separation of Vue+SpringBoot

Cross-domain issues in front-end and back-end separation of Vue+SpringBoot

In the front-end and back-end separation development, the front-end needs to call the back-end API and display the content. If the front-end and back-end development are both on the same host, due to the browser's homology policy restrictions, cross-domain problems (different protocols, domain names, port numbers, etc.) will arise, resulting in the inability to call the API interface normally, causing inconvenience to development.

Encapsulating API requests

import axios from 'axios'

//axios.create creates an axios instance and writes configuration for the instance. All subsequent requests sent through the instance are subject to the current configuration. const $http = axios.create({
    baseURL: '',
    timeout: 1000,
    //headers: {'X-Custom-Header': 'foobar'}
});

// Add request interceptor $http.interceptors.request.use(function (config) {
    // Do something before sending the request return config;
}, function (error) {
    // Do something with request error return Promise.reject(error);
});

// Add response interceptor $http.interceptors.response.use(function (response) {
    // Do something with the response data return response.data; // Return the data part of the response data }, function (error) {
    // Do something with the error return Promise.reject(error);
});

export default $http

API call function

export const getCourses = () => {
return $http.get('http://localhost:8080/teacher/courses')
} 

In this example, the front-end uses port 8081, the back-end uses port 8080, and the front-end fails to request data by calling the API.

Postman tests that this API interface is normal

How to solve the homology problem?

1. Create a new vue.config.js file in the vue root directory and configure it

vue.config.js file

module.exports = {
    devServer: {
        host: 'localhost', //Host number port: 8081, //Port number open: true, //Automatically open the browser proxy: {
            '/api': {
                target: 'http://localhost:8080/', //Interface domain name changeOrigin: true, //Is it cross-domain? ws: true, //Is it a proxy for websockets?
                secure: true, //Whether https interface pathRewrite: { //Path reset '^/api': '/'
                }
            }
        }
    }
};

2. Modify API request

API call function

export const getCourses = () => {
  return $http.get('/api/teacher/courses')
}

Here, because vue.config.js configures the interface domain name, the url here only needs to write the rest

URL full body

http://localhost:8080/teacher/courses

But here, because of the use of proxy, add '/api' before the remaining part (i.e. '/teacher/courses') to form '/api/teacher/courses'

At this point, the cross-domain problem is solved, and the front-end can get data from the back-end interface and display it

Problem solved!

This is the end of this article about the cross-domain problem in the front-end and back-end separation of Vue+SpringBoot. For more relevant vue SpringBoot front-end and back-end separation cross-domain content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed steps for front-end and back-end separation login authentication of SpringBoot+Vue+JWT
  • Springboot+VUE front-end and back-end separation to realize the epidemic prevention platform JAVA
  • Vue combined with Springboot to realize the user list single page (front and back end separation)
  • Detailed explanation of Vue's front-end system and front-end and back-end separation

<<:  The easiest way to create a new user and grant permissions to MySQL

>>:  Linux jdk installation and environment variable configuration tutorial (jdk-8u144-linux-x64.tar.gz)

Recommend

Detailed explanation of Linux DMA interface knowledge points

1. Two types of DMA mapping 1.1. Consistent DMA m...

Collection of 12 practical web online tools

1. Favicon.cc To create ico icon websites online,...

Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14

1. RTMP RTMP streaming protocol is a real-time au...

Specific use of Linux gcc command

01. Command Overview The gcc command uses the C/C...

10 very good CSS skills collection and sharing

Here, clever use of CSS techniques allows you to g...

Detailed explanation of CSS text decoration text-decoration &amp; text-emphasis

In CSS, text is one of the most common things we ...

JS implements circular progress bar drag and slide

This article example shares the specific code of ...

Detailed steps to install CentOS7 system on VMWare virtual machine

Pre-installation work: Make sure vmware workstati...

How to implement email alert in zabbix

Implemented according to the online tutorial. zab...

Write a React-like framework from scratch

Recently I saw the article Build your own React o...

WeChat applet example of using functions directly in {{ }}

Preface In WeChat applet development (native wxml...

Vue uses three methods to refresh the page

When we are writing projects, we often encounter ...

CentOS system rpm installation and configuration of Nginx

Table of contents CentOS rpm installation and con...

MySQL character types are case sensitive

By default, MySQL character types are not case-se...