vue.js 无法从前端连接到后端Web API

p5cysglq  于 7个月前  发布在  Vue.js
关注(0)|答案(1)|浏览(73)

我正在创建一个全栈应用程序,当我尝试连接到我的后端时,我得到一个CORS错误,但我已经允许在我的Web API中链接。
这是我的Web API program.cs

using Api.Data;

namespace Api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddScoped<DataContext>();
            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            builder.Services.AddCors(options =>
            {
                options.AddPolicy("DevCors",
                        builder =>
                        {
                            builder.WithOrigins("http://localhost:8080")
                                .AllowAnyMethod()
                                .AllowAnyHeader()
                                .AllowCredentials();
                        }
                    );
                options.AddPolicy("ProdCors",
                        builder =>
                        {
                            builder.WithOrigins("http://e-estates.com","https://e-estates.com")
                                .AllowAnyMethod()
                                .AllowAnyHeader()
                                .AllowCredentials();
                        }
                    );
            });

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseCors("DevCors");

                app.UseSwagger();
                app.UseSwaggerUI();
            }
            else
            {
                app.UseCors("ProdCors");
                app.UseHttpsRedirection();
            }
            
            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}

字符串
这是我尝试在Vue.js中连接axios的前端:

<script>
// import axios from "@/Backend/Api/axios";
import shaxios from "axios"

export default {
  name: "RegisterPage",
  data() {
    return {
    }
  },
  methods: {
    onSubmit() {
      // axios.Register({
      //     Email: this.email,
      //     Firstname: this.firstName,
      //     Lastname: this.lastName,
      //        Phone: this.phone,
      //     AccountType: this.accountType,
      //     IBAN: this.iban,
      //     PIN: this.pin,
      //     Password: this.password
      //   })
      //     .then(r => {
      //       console.log(r)
      //     })

      shaxios.get("https://localhost:5241/api/Auth/testconnection")
    }
  }
}
</script>


这是我得到的错误:
获取https://localhost:5241/api/Auth/testconnection
net::ERR_SSL_PROTOCOL_ERROR

igsr9ssn

igsr9ssn1#

看起来你在axios调用中使用了https,这需要ssl(因此你得到了ssl错误),但看起来你并没有在本地设置它。

相关问题