代码之家  ›  专栏  ›  技术社区  ›  A191919

来自web api控制器的角6响应“字符串”

  •  0
  • A191919  · 技术社区  · 7 年前

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

    在控制台中 result 我明白了

    SyntaxError:输入意外结束
    在app component.push../src/app/app.component.ts.AppComponent.setTitle( http://localhost:4200/main.js:137:14
    在Object.eval[作为handleEvent](ng:///AppModule/AppComponent.ngfactory.js:13:27)
    手边通风口( http://localhost:4200/vendor.js:67178:41
    在callWithDebugContext( http://localhost:4200/vendor.js:68272:25 )
    位于Object.debugHandleEvent[作为handleEvent]( http://localhost:4200/vendor.js:67975:12 )
    http://localhost:4200/vendor.js:64627:25 )
    http://localhost:4200/vendor.js:65074:38
    在htmlButtoneElement。( http://localhost:4200/vendor.js:74051:36
    在ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask( http://localhost:4200/polyfills.js:2766:31 )
    http://localhost:4200/vendor.js:61652:33 )

    如何强制角6识别字符串?

    启动.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();
            }
        }
    

    应用组件

    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;
      }
    }
    

    [Route("api/[controller]")]
    public class HomeController : Controller
    {
        // GET api/<controller>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }
    }
    
    0 回复  |  直到 7 年前
    推荐文章