第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何修復(fù) Angular/.NET 項(xiàng)目中的“跨源請求被阻止:同源策略不允許讀取遠(yuǎn)程資源”

如何修復(fù) Angular/.NET 項(xiàng)目中的“跨源請求被阻止:同源策略不允許讀取遠(yuǎn)程資源”

C#
牛魔王的故事 2022-11-21 21:18:40
當(dāng)我向我們的 API 端點(diǎn)提交 HTTP GET 請求時(shí),我收到錯誤消息,指出缺少 CORS 標(biāo)頭“Access-Control-Allow-Origin”。我們有一個(gè)連接到帶有 Angular 7 前端的 .NET API 的 SQL Server 數(shù)據(jù)庫。這是提交請求的 Angular 服務(wù)public VALUES_API_ENDPOINT = 'http://localhost:53273/api/search';  constructor(private httpClient: HttpClient) { }  async queryResults(terms: string): Promise<any> {    try {      const result = await this.httpClient.get(this.VALUES_API_ENDPOINT + '/title/' + terms).toPromise();      return result;    } catch (error) {        await this.handleError(error);    }  }后端控制器調(diào)用如下所示:[HttpGet][Route("title")]public HttpResponseMessage SearchByTitle(string s){   HttpResponseMessage response;   try   {       List<Regulation> results = search.SearchRegulationByTitle(s);       string json = CondensedRegulationsToJson(results);       response = Request.CreateResponse(HttpStatusCode.OK);       response.Content = new StringContent(json, Encoding.UTF8, "application/json");   }   catch (Exception)   {       response = Request.CreateResponse(HttpStatusCode.InternalServerError);   }   return response;}我們的 API 配置如下所示:public static void Register(HttpConfiguration config){   // Web API configuration and services   // Web API routes   config.MapHttpAttributeRoutes();   config.Routes.MapHttpRoute(         name: "DefaultApi",         routeTemplate: "api/{controller}/{id}",         defaults: new { id = RouteParameter.Optional }   );   // Enable CORS for the Angular App   // NOTE: Since we are running both the frontend and the backend locally, we can trust this origin with all headers and methods (the *). In practice, we would only allow really needed headers and methods    config.EnableCors(new EnableCorsAttribute("http://localhost:4200", "*", "*", "*"));  // Make JSON the default formatter over XML  config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();            config.Formatters.Remove(config.Formatters.XmlFormatter);}我們應(yīng)該從數(shù)據(jù)庫接收數(shù)據(jù),但我們只是根據(jù) CORS 屬性被拒絕訪問。
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

您的 api 網(wǎng)址不正確:

您還有其他/new/導(dǎo)致 404 錯誤的原因。從 api url中取出/new,問題應(yīng)該得到解決。

 const result = await this.httpClient.get(this.VALUES_API_ENDPOINT + '/title/' + terms).toPromise();



查看完整回答
反對 回復(fù) 2022-11-21
  • 1 回答
  • 0 關(guān)注
  • 96 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號