In this Blazor WASM application I am simply trying to consume a third party API - I open up developer tools and see this message "CORS Missing Allow Origin" when I attempt to retrieve the data. In this case I did add request.Headers.Add("Access-Control-Allow-Origin", "*");
to the request but it is still failing. I have verified this result in different browsers other than FF.
The code for the Index.Razor page is here:
@page "/" @inject HttpClient Http <h3>Presentations Scheduled</h3> <form> <fieldset class="form-group"> <div> <button type="button" class="btn btn-dark mr-sm-2 mb-2" @onclick=@(async ()=> await GetToday())> Today </button> </div> </fieldset> @if (string.IsNullOrWhiteSpace(errorString) == true) { <div class="h2">No Data</div> } else if (string.IsNullOrWhiteSpace(errorString) == false) { <div class="h2">@errorString</div> } </form> <div class="h2">@TestReturn</div> @code { TestModel TestReturn; string errorString = null; string TestBaseURL; HttpRequestMessage request; private async Task GetToday() { var byteArray = Encoding.ASCII.GetBytes("user:pass"); Http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); string todayDate = DateTime.Today.AddDays(0).ToString("yyyyMMdd"); TestBaseURL = "https://webservices.xcompany.com/"; UriBuilder TestURI = new UriBuilder(TestBaseURL); TestURI.Scheme = "https"; TestURI.Path = "ws/run/reservations.json"; TestURI.Query = "resource_query_id=246492"; var TestQuery = HttpUtility.ParseQueryString(TestURI.Query); TestQuery["scope"] = "extended"; TestQuery["start_dt"] = todayDate; TestQuery["end_dt"] = todayDate; TestURI.Query = TestQuery.ToString(); TestBaseURL = TestURI.ToString(); request = new HttpRequestMessage(HttpMethod.Get, TestBaseURL); request.Headers.Add("Access-Control-Allow-Origin", "*"); try { using var httpResponse = await Http.SendAsync(request); TestReturn = await httpResponse.Content.ReadFromJsonAsync<TestModel>(); } catch (Exception ex) { errorString = $"There was a problem: {ex.Message}"; } } }
https://stackoverflow.com/questions/65401899/blazor-wasm-networkerror-when-attempting-to-fetch-resource-cors-missing-allow December 22, 2020 at 08:48AM
没有评论:
发表评论