前端vue3 + axios 后端 express 跨域问题

一、前端vue 中 axios简单封装

import axios from "axios";
const http = axios.create({
  baseURL: '',
  timeout: 1000 * 60 * 2,
  withCredentials: true,
})


http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
export default http

二、express 中设置

const express = require('express')
const cors = require('cors')

const app = express()
const corsOptions = {
    origin:'http://localhost:8080', //axios中设置了withCredentials: true,这里origin需指定前端的具体网址
    credentials: true, //允许携带cookie
}
app.use(cors(corsOptions))

app.listen(3000, () => {
    console.log('http://localhost:3000')
})

你可能感兴趣的:(前端vue3 + axios 后端 express 跨域问题)