【问题标题】:Why I get net::ERR_CONNECTION_RESET message?为什么我收到 net::ERR_CONNECTION_RESET 消息?
【发布时间】:2021-06-24 17:54:08
【问题描述】:

我正在开发一个 ASP.NET Web Api,我编写了一些代码来测试我的一个 api 控制器。但是当我调用该方法时,chrome 会发送这个:

http://localhost:44333/api/GradesReader/getTest/net::ERR_CONNECTION_RESET jquery.min.js:2

这是我的 API 项目 Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }

    


    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(x => x.Filters.Add(new AuthorizeFilter())).AddXmlSerializerFormatters()
                          .AddXmlDataContractSerializerFormatters();

        //For the personal exception handling...
        //services.AddMvc(config =>
        //{
        //    config.Filters.Add(typeof(DiaryExceptionFilter));
        //});

        services.AddDbContext<DiaryDataContext>();
        

        services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<DiaryDataContext>()
                .AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(opt =>
        {
            opt.Events = new CookieAuthenticationEvents
            {
                OnRedirectToLogin = redirectContext =>
                {
                    redirectContext.HttpContext.Response.StatusCode = 401;
                    return Task.CompletedTask;
                },
                OnRedirectToAccessDenied = redirectContext =>
                {
                    redirectContext.HttpContext.Response.StatusCode = 401;
                    return Task.CompletedTask;
                }
            };
        });

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });

        services.AddCors(); //Ezt raktuk bele!
    }
   

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            //app.UseExceptionHandler("/Home/Error");
            //// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            //app.UseHsts();
            app.UseExceptionHandler(
           options =>
           {
               options.Run(async context =>
               {
                   context.Response.StatusCode = 500;
                   context.Response.ContentType = "application/json";
                    //var ex = context.Features.Get<IExceptionHandlerFeature>();
                    //if (ex != null)
                    //{
                    //    await context.Response.WriteAsync(ex.Error.Message);
                    //}
                });
           });
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        
        //app.UseEndpoints(endpoints =>
        //{
        //    endpoints.MapControllerRoute(
        //        name: "default",
        //        pattern: "{controller=Home}/{action=Index}/{id?}");
        //});
        app.UseEndpoints(endpoints =>                                    
        {
            endpoints.MapControllers();
        });
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

        app.UseCors(opt => opt.WithOrigins("https://localhost:44333/"));


    }
}

方法如下:

[HttpGet("getTest")]
    public string GetTest()
    {
        return "TEST";
    }

这里是html:

`

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="lib/jquery/dist/jquery.min.js"></script>
    <script src="js/jquery.unobtrusive-ajax.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#myButton").click(function () {
                $.ajax({
                    url: 'http://localhost:44333/api/GradesReader/getTest/',
                    type: 'GET',
                    /*cache: false,*/
                    /*dataType = 'json',*/
                    success: function (result) {
                        console.log(result);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <h1>Test API</h1>

    <input type="button" id="myButton" value="Show Grades" />

</body>
</html>

`

【问题讨论】:

  • 这是一条浏览器错误消息,很可能表明存在网络或协议问题。当您尝试不同的浏览器时会发生什么?

标签: javascript html ajax asp.net-core connection


【解决方案1】:
url: 'http://localhost:44333/api/GradesReader/getTest/',

在您的应用程序中,您已将其配置为使用https 请求,但上述请求网址是http 请求。所以,它会显示net::ERR_CONNECTION_RESET 错误。

要解决此问题,请将 URL 更改为使用https

   url: 'https://localhost:44333/api/GradesReader/getTest/',

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2015-08-08
    • 2022-01-21
    相关资源
    最近更新 更多