javascript 如何使用javascript或jquery加载html div标签中的url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15427109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:44:28  来源:igfitidea点击:

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

javascriptjqueryhtml

提问by Hari

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.

在这里,我编写了以下内容,用于通过我附上的“url”加载 div 标签内的网页。但它不起作用。谁能知道。请帮忙。

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

回答by Talha

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

你不能使用 jQuery 的 ajax 方法从外部域获取数据,而不使用一种Proxy, YQL, JSONP或等效的技术来解决这个问题。浏览器限制,大部分 Ajax 请求都遵循“同源策略”。

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

您可以使用https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js并将 js 文件包含在您的页面中。下面是我用来加载外部页面的函数。

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

回答by Dipesh Parmar

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

没错,出于安全原因,ajax 调用无法检索外部页面。

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.

将外部页面添加到您的页面的唯一方法是使用 iframe,或您可以使用 ajax 调用的简单服务器端代理。

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

除非内容来自同一个域,否则我们不能这样做,并且是 javaScript 的同源策略的原因。

You can still do it as

你仍然可以这样做

  1. use iframe load the content
  2. use server-side script
  1. 使用 iframe 加载内容
  2. 使用服务器端脚本

回答by Samy

Before trying out the answers..You have to add http: in your src path.

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

回答by Edgar Allan Pwn

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.

除非您访问的文件与您的页面位于同一台服务器上,否则这将不起作用。如果您尝试嵌入外部内容,则需要使用 iframe。

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

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