简体   繁体   中英

How to load the url in html div tag using javascript or jquery

Here the below i have wriiten for load the webpage inside of div tag via 'url' i've enclosed. but it not working. can anyone know. pls help.

<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<a href="#" onClick="lurl();">Click Here</a>
<br/>
<br/>
<br/><div id="durl">
</div>
<script type="text/javascript">
function lurl(){
$('#durl').load('http://www.tndte.com/Result/');
}
</script>
</html>

you can't use jQuerys ajax methods to fetch data from external domains without using a Proxy, YQL, JSONP or equivalent technique to get around this. Browser restricts, most Ajax requests are subject to the " same origin policy ".

You can use the https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js and include the js file in your page. Below is the function that I used to load the external page.

function test () {
         $.ajax({
           url: 'http://www.tndte.com/Result/',
           type: 'GET',
           success: function(res) {
             var content = $(res.responseText).text();
             alert(content);
           }
         });
       }

Thats correct, ajax calls cannot retrieve external pages for security reasons.

The only way to get external pages onto your page is to use an iframe, or a simple server side proxy that you can call with your ajax.

We can not do that unless the content are coming from the same domain , and reason javaScript's Same Origin Policy.

You can still do it as

  1. use iframe load the content
  2. use server-side script

Unless you are accessing files on the same server as your page is, this will not work. You need to use an iframe if you're trying to embed external content.

http://www.w3.org/TR/html4/present/frames.html

在尝试答案之前..你必须在你的src路径中添加http:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM