首页 > 解决方案 > 使用 Blazor WASM 从外部 API 获取数据

问题描述

我尝试使用 blazor WASM 从外部 API 获取数据,但出现异常:“TypeError: NetworkError when trying to fetch resource” 我的代码如下所示。有人能告诉我我应该怎么做才能得到这些数据吗?先感谢您。

    public class Rate
        {
        public string no { get; set; }
        public string effectiveDate { get; set; }
        public double mid { get; set; }
        }

    public class Nbp
        {
        public string table { get; set; }
        public string currency { get; set; }
        public string code { get; set; }
        public Rate[] rates { get; set; }
        }
//...
var test = await Http.GetFromJsonAsync<Nbp>("http://api.nbp.pl/api/exchangerates/rates/A/EUR/2020-12-15/?format=json");

标签: blazorblazor-webassemblyblazor-client-side

解决方案


将您的请求更改为https.

var test = await Http.GetFromJsonAsync<Nbp>("https://api.nbp.pl/api/exchangerates/rates/A/EUR/2020-12-15/?format=json");

推荐阅读