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

如何调试“http失败响应(未知url):0未知错误”[重复]

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

    我的API操作:

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

    洛坎

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

    洛恩斯

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

    贷款.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() {
      }
    }
    

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

    enter image description here

    我怎么才能搞清楚这件事呢?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Kristoffer Jälén    7 年前

    控制台说请求由于cors而被阻止。您需要将CORS支持添加到服务器: https://enable-cors.org/server.html .

    了解更多关于CORS的信息: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors .