How to read external html file and store it to string variable using jQuery? Showing Error

为君一笑 提交于 2021-02-07 19:22:40

问题


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..?

HTML File - 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

$(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

awaiting responses.
Thanks in Advance


回答1:


Changed JQUERY Code

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

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



回答2:


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



回答3:


using jquery

$.get("http://localhost/something/something.html", function (result) {
     html = result;
     text = $(result).text();
});


来源:https://stackoverflow.com/questions/39051658/how-to-read-external-html-file-and-store-it-to-string-variable-using-jquery-sho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!