简体   繁体   中英

Http Client Subscribe Never Hit

I am using Angular 7 which is calling ac# core endpoint. The http request resides in client.service.ts, and is called from a button click event handler in my home.component.ts.

The subscribe method is simply never being called. I've gotten http request to ork in Angular in the past, so I'm not sure what I'mdoing differently. I think I've tried 20 different variations, and to no avail, the subscribe method is never called.

I know the http call is working. The called c# core service receives the request and returns the object value no problem. But back in the Angular app, subscribe is just skipped over.

Here is the method that calles the http client service in the home.component.ts class:

    onClickSimpleBtn() {

    this.clientService.diffConfigsByObject().subscribe((report: Report) => {

        this.serviceName = report.serviceName;

    });
}

Here is the method that receives the previous call and performs the http request in client.service.cs:

    diffConfigsByObjectClean(): Observable<Report> {

    let url = `https://localhost:44392/DiffConfigsByObject`;

    return this.http.get<Report>(url);
}

The receiving c# endpoint is as follows (again, this part functions correctly):

    [HttpGet]
    public Report Get()
    {
        var report = new Report();
        return report;
    }

Please help!! Been stuck on this for 2 days now. If any additional code is needed don't hesitate to ask. Thanks!

After checking the console it appears CORS is blocking the response only. Odd. I have arrangements to enable CORS, and the outbound request works fine. But it appears the return value is not working. I thought once the outbound call was picked up in the opposing service, that I was in the clear regarding CORS. I guessed wrong.

The solution was:

The c# core service that hosted the called endpoint required CORS setup as well. Up to this point I had only arranged CORS on the calling angular service.
The receiving c# service needed the following:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddCors();
        ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...

        app.UseCors(options =>
            options.WithOrigins("https://localhost:44395")
            .AllowAnyMethod()
            .AllowAnyHeader()
        );

        ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM