簡體   English   中英

如何在調用 API 時使用 DOMSANITIZER(bypassSecurityTrustUrl)

[英]How to use DOMSANITIZER(bypassSecurityTrustUrl) while calling the API

在調用 API 以獲取數據時獲取 XSS 漏洞。 所以試圖添加DOMSANITIZER ,但它失敗了。 試過下面的代碼,請給我建議解決方案。

 this.http.get(this.domSanitizer.bypassSecurityTrustUrl(dataUrl),{headers:headers}).subscribe(response => {
      this.persons = response.data.map(x=>({...x,check:false,test:x.firstName}));
      this.dtTrigger.next();
    });

閃電戰

您可以通過以下方式在使用 API 時使用 DOMSANITIZER。

  1. 導入這些:
 import { Component, OnInit, ViewChild, SecurityContext, } from '@angular/core'; import {DomSanitizer} from '@angular/platform-browser';
  1. 在您使用它的項目中使用以下代碼:
 const dataUrl = this.domSanitizer.sanitize( SecurityContext.RESOURCE_URL, this.domSanitizer.bypassSecurityTrustResourceUrl( 'https://raw.githubusercontent.com/l-lin/angular-datatables/master/demo/src/data/data.json' ) ); this.http.get(dataUrl).subscribe((response) => { this.persons = response.data.map((x) => ({ ...x, check: false, test: x.firstName, })); this.dtTrigger.next(); });

重要的:

此代碼適用於您的 stackblitz 網址。

在此處輸入圖片說明

我也保存了,你可以去那里查看。 https://stackblitz.com/edit/column-names-as-tooltip-wcw1f7?file=app%2Fapp.component.ts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM