vue.js - Vue & Axios:为什么我设置了代理后仍然出现 CORS 错误?
问题描述
向我的 vue 应用程序添加代理后,我仍然收到 CORS 错误。
在我的vue.config.js 上,它看起来像这样。
module.exports = {
devServer: {
proxy: {
"/api": {
target: "http://website.com/",
changeOrigin: true
}
}
}
}
当我使用 axios 调用我的 api 时,我的代码如下所示
const response = await axios.post(`/api/Inbound/getData`,dataBody);
然而,我仍然对 CORS 有疑问,这就是控制台上显示的内容。
从源“http://localhost:8080”访问“https://website.com/api/Inbound/getData”(从“http://localhost:8080/api/Inbound/getData”重定向)的 XMLHttpRequest被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态
我还设置了 IIS 以允许使用Startup.cs上的以下代码从跨源源接收数据
app.UseCors(builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials());
并编辑我的Web.config文件以具有以下几行
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
任何想法为什么会发生这个问题都会很棒。
编辑 1:添加startup.cs文件
public class Startup
{
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public Startup(IConfiguration configuration)
{
_config = configuration;
StaticConfig = configuration;
}
public IConfiguration _config { get; }
public static IConfiguration StaticConfig { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddDbContextPool<OutboundDBContext>(options => options.UseSqlServer(_config.GetConnectionString("APICon")));
services.AddDbContextPool<InBoundDBContext>(options => options.UseSqlServer(_config.GetConnectionString("APICon")));
services.AddDbContextPool<ManagementDBContext>(options => options.UseSqlServer(_config.GetConnectionString("APICon")));
services.AddMvc().AddXmlSerializerFormatters();
services.AddScoped<IOutboundData, OutboundSQLData>();
services.AddScoped<IInboundData, InboundSQLData>();
services.AddScoped<IManagementData, ManagementSQLData>();
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Web Api", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web Api"));
}
app.UseCors(builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed(_ => true)
.AllowCredentials());
app.UseHttpsRedirection();
app.UseRouting();
//app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
解决方案
推荐阅读
- python-3.x - Tensorboard 投影仪可视化 - PCA 不断加载
- javascript - 我有这样的 JSON 字符串化数据:
- android - 如何将图像移动到线性布局的底部
- go - 更好地理解 Kademlia 的 XOR 整数度量
- python - 插入均值后 numpy 数组形状发生变化
- oracle - 在 oracle Apex 中将金额格式设置为 $xxx,xx
- php - 如何在 PHP 中使用八进制、十六进制和 unicode 代码点表示法打印一个字母?
- java - 如何构建没有特定类型的堆排序类?
- c - 无法将 char 字符串插入 char 数组
- ros - Pepper 机器人安装错误( naoqi_driver: No definition of [boost] for OS version [] )