【问题标题】:Http request not reaching MVC Backend. No Errors Showing WhyHttp 请求未到达 MVC 后端。没有错误说明原因
【发布时间】:2019-07-10 22:36:13
【问题描述】:

我正在尝试在 Angular 中重新创建《英雄之旅》教程程序,但这次使用的是 MVC 后端。但是,当我尝试获取英雄列表时,我的 HTTP 获取请求和服务器之间似乎断开了连接。我不确定是什么导致了这个问题,因为当我错误地应用程序时没有任何错误输出显示。 (前端和后端都分别从 VSC 和 VS 运行)。你们可以提供的任何输入都会很棒。

下面是我的英雄服务和英雄控制器的代码。

     import { Injectable } from '@angular/core';
     import { HttpClient, HttpHeaders } from '@angular/common/http';

     import { Observable, of } from 'rxjs'; 
     import { catchError, map, tap } from 'rxjs/operators';

     import { Hero } from './heroes';
     import { MessageService } from './messages.service';

     const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
      };

@Injectable()
export class HeroService {

  private heroesUrl = 'http://localhost:44339/api/values';  // URL to web api

  constructor( private http: HttpClient, private messageService:    MessageService) { }

  /** GET heroes from the server */
  getHeroes (): Observable<Hero[]> {
    return this.http.get<Hero[]>(this.heroesUrl);

  }

}
``````````````
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HeroApi.Models;
using Microsoft.AspNetCore.Mvc;

namespace HeroApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HeroesController : ControllerBase
    {

        [HttpGet]
        public IEnumerable<Hero> GetAll()
        {
            var list = new List<Hero>();
            list.Add(new Hero() { name = "Dynamo" , id = 1});
            list.Add(new Hero() { name = "Captain Fission", id = 2 });
            return list;
        }


        [HttpGet("{id}")]
        public Hero Get(int Id)
        {
            return new Hero(){ id = Id };
        }


        [HttpPost]
        public void Post([FromBody] string value)
        {
        }


        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }


        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
``````````````

【问题讨论】:

  • 浏览器控制台可以显示所有客户端 XHR。您在那里看到任何请求吗?
  • 以下警告是来自控制台的唯一输出 **client:148 [WDS] 编译时的警告。警告@ client:148 ./node_modules/@angular/http/src/backends/xhr_backend.js 167:24-52 “在“@angular/platform-b​​rowser”中找不到“导出”platform_browser_private' **@client 154
  • 如果您收到警告,请将它们添加到您的问题中。 (另外,您可能需要在控制台的过滤器设置中打开 XHR)
  • 能否请您尝试将api URL更新为私人英雄Url = 'localhost:44339/api/Heroes/GetAll';
  • 对不起,我的意思是在发布之前将其切换为“英雄”而不是“价值”,但我忘记修复我的初始帖子以包含该内容。在 URL 末尾包含 GetAll 似乎不起作用。结果相同

标签: javascript asp.net-mvc angular model-view-controller


【解决方案1】:

您是否订阅了从 getHeroes 返回的 Observable?

heroService.getHeros()

不会触发 http 请求,它会返回一个在订阅时触发 http 请求的 observable。

heroService.getHeros().subscriber(heros => {
  // do stuff with heroes, this will fire off a http request
});

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多