简体   繁体   English

如何在调用 API 时使用 DOMSANITIZER(bypassSecurityTrustUrl)

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

Getting XSS vulnerabilities while calling the API for fetching the data.在调用 API 以获取数据时获取 XSS 漏洞。 So trying to add DOMSANITIZER , but its failing.所以试图添加DOMSANITIZER ,但它失败了。 Tried below code, please suggest me the solution.试过下面的代码,请给我建议解决方案。

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

Stackblitz 闪电战

You can use DOMSANITIZER while using the API in following way.您可以通过以下方式在使用 API 时使用 DOMSANITIZER。

  1. Import these:导入这些:
 import { Component, OnInit, ViewChild, SecurityContext, } from '@angular/core'; import {DomSanitizer} from '@angular/platform-browser';
  1. Use this below code in your project where you are using this:在您使用它的项目中使用以下代码:
 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(); });

Important:重要的:

This code is working on your stackblitz url.此代码适用于您的 stackblitz 网址。

在此处输入图片说明

I have also save it and you can go there to check it.我也保存了,你可以去那里查看。 https://stackblitz.com/edit/column-names-as-tooltip-wcw1f7?file=app%2Fapp.component.ts https://stackblitz.com/edit/column-names-as-tooltip-wcw1f7?file=app%2Fapp.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用<mat-progress-bar>在 .ts 中,同时使用 angular 调用 API 服务 - How to use <mat-progress-bar> in .ts while calling the API service using angular 如何将DomSanitizer作为打字稿中的参数传递给管道? - How to pass DomSanitizer as an argument in typescript to a pipe? 进行 DomSanitizer API 调用时不使用基本属性? - Not using the base attribute when making DomSanitizer API calls? Angular2 innerHTML删除属性,帮助使用DomSanitizer - Angular2 innerHTML removes property, help needed to use DomSanitizer 调用api时如何以角度获取错误响应? - how to get error response in angular while calling an api? 使用DomSanitizer后,图片网址仍然不安全 - Image url still unsafe after I use DomSanitizer 使用 DomSanitizer 绕过安全性后,安全值必须使用 [property]=binding - Safe value must use [property]=binding after bypass security with DomSanitizer 我如何去测试依赖于 DomSanitizer 的管道? - How do I go about testing a Pipe which depends on DomSanitizer? Angular 9:如何使用 DomSanitizer 显示带有数据属性的 HTML/CSS - Angular 9: How to display HTML/CSS with data attributes using DomSanitizer Angular 6 中的 CORS 问题,同时调用 API - CORS issue in Angular 6, while calling the API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM