加载并附加外部html文件

时间:2021-10-29 13:54:57

I want to load external HTML file with a content like this:

我想加载外部HTML文件,其内容如下:

{"html":" \n <font>\u043b\u0432.<\/font> \n ","back":""}

I tried with this code - it loads the file but it remains with \n and the other things..

我试过这个代码 - 它加载文件,但它仍然与\ n和其他东西..

<div id="success"></div>
<script>$.get('test.html', function(data) { $(data).appendTo("#success"); } );</script>

2 个解决方案

#1


1  

Modified code: Since your data is a JSON object you should use $.getJSON . and replace all \n with <br>.

修改后的代码:由于您的数据是JSON对象,因此您应该使用$ .getJSON。并用
替换所有\ n。

<script>
   $.getJSON('test.html', function(data) {
      data =data.html.replace(/\n/g, "<br>"); 
      $(data).appendTo("#success"); } );
</script>

#2


0  

Instead of using appendTo, do the following:

而不是使用appendTo,请执行以下操作:

<div id="success"></div>
<script>$.get('test.html', function(data) { $("#success").html(data); } );</script>

Please beware: If you don't control the contents of "data", you're exposing yourself to XSS attacks. If you do, it is ok.

请注意:如果您不控制“数据”的内容,那么您将面临XSS攻击。如果你这样做,那就没关系。

#1


1  

Modified code: Since your data is a JSON object you should use $.getJSON . and replace all \n with <br>.

修改后的代码:由于您的数据是JSON对象,因此您应该使用$ .getJSON。并用
替换所有\ n。

<script>
   $.getJSON('test.html', function(data) {
      data =data.html.replace(/\n/g, "<br>"); 
      $(data).appendTo("#success"); } );
</script>

#2


0  

Instead of using appendTo, do the following:

而不是使用appendTo,请执行以下操作:

<div id="success"></div>
<script>$.get('test.html', function(data) { $("#success").html(data); } );</script>

Please beware: If you don't control the contents of "data", you're exposing yourself to XSS attacks. If you do, it is ok.

请注意:如果您不控制“数据”的内容,那么您将面临XSS攻击。如果你这样做,那就没关系。