如何读取外部html文件并使用jQuery将其存储到字符串变量?显示错误

时间:2021-03-18 21:49:06

I am trying to Dynamically Generating the Codes for Web Application
I Want to read External HTML File and store it to string variable in Javascript or Jquery ?
Is there any efficient way for this..?

我试图动态生成Web应用程序的代码我想读取外部HTML文件并将其存储到Javascript或Jquery中的字符串变量?这有什么有效的方法..?

HTML File - Object-text.html

HTML文件 - Object-text.html

<div class="text-object">
    <div class='text-area'>
        <span class='content'>
            <span class='title'> title</span><br/>
            <span class='address'>  address  </span>
        </span>
    </div>
    <div class='patch'></div>
</div>
<br class='float-clear'/>

JQUERY

JQUERY

$(document).ready(function()
{
  $.get("templates/object-text.html", function(html_string)
   {
      alert(html_string);  // this is not Working
   });
});

Showing Error in Console

在控制台中显示错误

Junk After Docuement Element

Docwement元素之后的垃圾

awaiting responses.
Thanks in Advance

等待回应。提前致谢

2 个解决方案

#1


4  

Changed JQUERY Code

更改了JQUERY代码

i've added the extra arguement to the function call $.get() to specify the html content

我已经在函数调用$ .get()中添加了额外的争论来指定html内容

$(document).ready(function()
{
  $.get("templates/object-text.html", function(html_string)
   {
      alert(html_string); 
   },'html');    // this is the change now its working
});

#2


1  

this verion is working fine for me you have to close `);` 
$(document).ready(function()
    {
      $.get("leads/leads_custom_box.php", function(result)
       {
          alert(result);
       });
    });

#1


4  

Changed JQUERY Code

更改了JQUERY代码

i've added the extra arguement to the function call $.get() to specify the html content

我已经在函数调用$ .get()中添加了额外的争论来指定html内容

$(document).ready(function()
{
  $.get("templates/object-text.html", function(html_string)
   {
      alert(html_string); 
   },'html');    // this is the change now its working
});

#2


1  

this verion is working fine for me you have to close `);` 
$(document).ready(function()
    {
      $.get("leads/leads_custom_box.php", function(result)
       {
          alert(result);
       });
    });