0

When I am making request to Web Api Controller

this.http.get("https://localhost:44354/api/home/5",{responseType: 'text' })
  .subscribe(result => { console.log(result); });

in console result I get

SyntaxError: Unexpected end of input↵
at AppComponent.push../src/app/app.component.ts.AppComponent.setTitle (http://localhost:4200/main.js:137:14)↵
at Object.eval [as handleEvent] (ng:///AppModule/AppComponent.ngfactory.js:13:27)↵
at handleEvent (http://localhost:4200/vendor.js:67178:41)↵
at callWithDebugContext (http://localhost:4200/vendor.js:68272:25)↵
at Object.debugHandleEvent [as handleEvent] (http://localhost:4200/vendor.js:67975:12)↵
at dispatchEvent (http://localhost:4200/vendor.js:64627:25)↵
at http://localhost:4200/vendor.js:65074:38
at HTMLButtonElement. (http://localhost:4200/vendor.js:74051:36)↵
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2766:31)↵
at Object.onInvokeTask (http://localhost:4200/vendor.js:61652:33)

How to force Angular 6 recognize string?

Startup.cs

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
            app.UseMvc();
        }
    }

app.component.ts

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  responsText: string;

  constructor(private http: HttpClient) {
  }

  setTitle(): void {
    this.http.get("https://localhost:44354/api/home/5",{responseType: 'text' })
      .subscribe(result => { console.log(result); });
    debugger;
  }
}

HomeController

[Route("api/[controller]")]
public class HomeController : Controller
{
    // GET api/<controller>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
A191919
  • 3,422
  • 7
  • 49
  • 93

0 Answers0