如何发送HTML字符串到后端?



我使用NextJs,我有一个富文本编辑器我的frontEnd。当我尝试将此html与axios请求发送到后端时,此输出是html。我得到错误,因为格式不是json。

我如何发送这个html到后端服务?有办法吗?

我下载了cheerio但是我看不懂

请尝试使用以下函数对HTML字符串进行编码:

const escapeHTML = function(str){
str.replace(/[&<>'"]/g, 
tag => ({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
}[tag]));
}

要解码HTML字符串,可以使用:

const decodeHTML = function(html) {
let txt = document.createElement('textarea');
txt.innerHTML = html;
return txt.value;
};

或:

const decodeHTML = function(input) {   
let doc = new DOMParser().parseFromString(input, "text/html");   
return doc.documentElement.textContent; 
}  
console.log(  decodeHTML("&lt;img src='img.jpg'&gt;")  )     
//Output: "<img src='img.jpg'>"

最新更新