跨域问题的前后端需要进行的设置

本文详细介绍了在跨域问题中,后端和前端需要进行的设置,包括后端允许跨域的配置,前端使用vue和fetch时如何设置允许跨域,并确保在跨域请求时能够携带参数,以便后端正确接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后端设置允许跨域

前端设置允许跨域

前端只是允许跨域还不够,还得设置跨域的时候允许携带参数不然后端接收不到参数

后端

package com.wlqk.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 2允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }
}

前端 使用vue 和 fetch

允许跨域 vue.config.js 中的配置
module.exports = {
    devServer: {
      proxy: {
        "/api": { //自定义
          target: "http://127.0.0.1:8089/api", //这里可以跟随项目实际部署服务器来
          changeOrigin: true,
          ws: true,
          pathRewrite: {
            "^/api": ""//自定义
          }
        }
      }
    }
  }
允许跨域携带参数 mode: “cors”
import HttpFetchImpl from './HttpFetchImpl'
import constUrl from './ConstUrl';

class Http {

    constructor(implement) {
        this.implement = implement;
    }

    post(url, data) {
        url = constUrl.getUrl()+url;
        let pojo = {
            method: "POST",
            body: JSON.stringify(data),
            mode: "cors",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            }
        }
        return this.implement.post(url,pojo);
    }

    get(url) {
        return this.implement.get(url);
    }

}
const http = new Http(HttpFetchImpl);
export default http;
fetch
class HttpFetchImpl{

    constructor() {

    }

    get (url) {
        return fetch(url)
                .then(resp => {
                    let data;
                    try {
                        data = resp.json();
                    } catch (e) {
                        data = resp.text();
                    }
                    return data;
                });
    }

    post(url, data) {
        return fetch(url,data).then(resp => {
            let data;
            try {
                data = resp.json();
            } catch (e) {
                data = resp.text();
            }
            return data;
        });
    }

}
const impl = new  HttpFetchImpl;
export default impl;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值