赞
踩
最近在完成一个项目的时候,在发送请求的时候,浏览器报错:Access to XMLHttpRequest at 'http://110.40.173.100:5173/api/user/login' from origin 'http://127.0.0.1:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
报错截图如下:
在vite.config.js文件中,进行以下配置:
- import vue from '@vitejs/plugin-vue'
- import Components from 'unplugin-vue-components/vite';
- // https://vitejs.dev/config/
- export default defineConfig({
- plugins: [
- vue(),
- Components({
- resolvers: [VantResolver()],
- })],
- //配置的内容
- server: {
- proxy: {
- '/api': {
- target: 'http://110.40.173.100:5173',//请求的地址
- changeOrigin: true,
- rewrite: (path) => path.replace(/^\/api/, '') // 可选,用于重写请求路径
- }
- }
- }
-
- })

- <script setup>
- import instance from '../utils/axios.js';//二次封装axios
- const onSubmit = async () => {
- await instance.post("/user/login", {
- username: state.username,
- password: state.password
- })
- };
- </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。