CORS跨域问题解决

.NET

复制代码
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//添加跨域策略1
builder.Services.AddCors(opts => {
    opts.AddPolicy("Cros", opt => opt.AllowAnyOrigin()
                                    .AllowAnyHeader()
                                    .WithExposedHeaders("X-Pagination"));
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();
//使用跨域策略2
app.UseCors("Cros");

app.Run();
复制代码

 

VUE前端(代理)

找到vue.config.js

 

 

复制代码
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
//代理
  devServer:{
    proxy:{
      '/api':{
          target:'http://localhost:5041/api',
          //允许跨域
          changeOrigin:true,
          ws:true,
          pathRewrite:{
            '^/api':""
          } 
      }
    }
  }
})
复制代码

需要重启程序!

import axios from "axios";
import {ref} from "vue";

const json=ref("/json");
const http=ref("/api"); 
export const getImage=()=>{
    return axios.get(http.value+"/Image/GetImage");
}

 

posted @   后跳  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2021-06-15 .Net 控制台动态刷新使用
点击右上角即可分享
微信分享提示