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

How to compile the Linux kernel

1. Download the required kernel version 2. Upload...

Why does using limit in MySQL affect performance?

First, let me explain the version of MySQL: mysql...

15-minute parallel artifact GNU Parallel Getting Started Guide

GNU Parallel is a shell tool for executing comput...

HTML table markup tutorial (15): table title

<br />This tag can be used to directly add a...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

Detailed explanation of the process of using GPU in Docker

Table of contents Download tf-gpu Build your own ...

Detailed explanation of map overlay in openlayers6

1. Overlay Overview Overlay means covering, as th...

Javascript to achieve the effect of closing advertisements

Here is a case study on how to close ads using Ja...

Tips for importing csv, excel or sql files into MySQL

1. Import csv file Use the following command: 1.m...

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

Example of how to implement keepalived+nginx high availability

1. Introduction to keepalived Keepalived was orig...

How to install kibana tokenizer inside docker container

step: 1. Create a new docker-compose.yml file in ...