从React-Native中的API获取HTML数据,HTML标签

时间:2019-10-21 13:54:58

标签: react-native axios

我正在使用React-Native制作移动应用程序,我想从我的API中获取数据,但是我的API的数据,数据库具有html标签(您可以在我共享的图像中看到它们)和一些字符代码,因为还有土耳其语字符。我已经在网站上使用此API了,没有问题,但是当我尝试将数据获取到我的react native应用程序时,它就像从图像中看到的那样以纯文本格式获取数据。这些数据没有问题enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

它称为HTML实体。这是适用于浏览器的代码(不适用于您的情况)

var decodeEntities = (function() {
  // this prevents any overhead from creating the object each time
  var element = document.createElement('div');

  function decodeHTMLEntities (str) {
    if(str && typeof str === 'string') {
      // strip script/html tags
      str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
      str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
      element.innerHTML = str;
      str = element.textContent;
      element.textContent = '';
    }

    return str;
  }

  return decodeHTMLEntities;
})();

代码是从此处复制的HTML Entity Decode

但是它仅在浏览器上有效,因为浏览器会自动为您翻译字符。


我不认为像这样传递未序列化的数据是一个好主意。 您可能希望使用url编码或其他方式在服务器端对数据进行编码,然后在react-native端对其进行解码


参考

答案 1 :(得分:0)

实际上,我通过以下方法解决了这个问题:https://github.com/archriss/react-native-render-html 谢谢