【问题标题】:How to debug "Http failure response for (unknown url): 0 Unknown Error" [duplicate]如何调试“(未知 url)的 Http 失败响应:0 未知错误”[重复]
【发布时间】:2018-12-09 02:09:39
【问题描述】:

我的 API 操作:

[HttpGet]
public ActionResult<IEnumerable<Loan>> Get()
{
    return _context.Loans;
}

Loan.cs

public class Loan
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string BorrowerName { get; set; }
    public decimal RepaymentAmount { get; set; }
    public decimal FundingAmount { get; set; }
}

Loan.ts

export class Loan {
    constructor(
        public Id : number,
        public BorrowerName : string,
        public RepaymentAmount : number,
        public FundingAmount : number
    ) { }
}

loans.component.ts

我可以看到http get 确实在调用我的 API,并且它正在返回预期的数据。

import { Component, OnInit } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import { Loan } from '../loan';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-loans',
  templateUrl: './loans.component.html',
  styleUrls: ['./loans.component.css']
})
export class LoansComponent implements OnInit {

  loans: Loan[];

  constructor(private http:HttpClient) {

    this.getLoans().subscribe((loans) => {
      this.loans = loans;
      console.log(loans);
    })
  }

  getLoans() {
    return <Observable<Loan[]>> this.http.get('http://localhost:1113/api/loans');
  }

  ngOnInit() {
  }
}

我在控制台中看到了这个错误:

我怎样才能弄清这个问题?

【问题讨论】:

    标签: c# angular asp.net-core-webapi


    【解决方案1】:

    控制台显示请求因 CORS 而被阻止。您需要为服务器添加 CORS 支持:https://enable-cors.org/server.html

    阅读有关 CORS 的更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors

    【讨论】:

    • 感谢 Kristoffer,感谢您的评论,我找到了this answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多