有没有办法显示另一个网站的表格

Is there a way to show a table from another website?

本文关键字:网站 表格 另一个 显示 有没有      更新时间:2023-09-26

有没有办法显示另一个网站的表。例如,我想要的表在example.com上,id为table。在我的html中,我有一个div,我也想在其中显示id为table的表。我真的不知道该怎么做。我已经研究了其他问题。但这些答案对我来说不起作用。我也读过一些关于"file_get_contents"的文章,但我不知道如何用它来获得特定的id。

这是.load()方法的API。它是一个jQueryajax函数。它能够在当前页面加载完成后加载内容。

http://api.jquery.com/load/

使用此代码将有助于将#table加载到#target中。请注意,ajax在某些浏览器(如Chrome)上无法在本地工作。如果本地网页不起作用,应该在服务器上进行测试

$("#target").load("http://example.com #table");

挠一下

感谢charlietfl,他指出您需要CORS来请求数据。老实说,我对此并不熟悉,但以前也处理过。在这里,我编译了一些可能对您有所帮助的代码:

$(function() {
	function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr){
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined"){
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
	}
  var req = createCORSRequest("GET", "https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/JavaScript");
  if (req) {
  	req.onload = function(data) {
    	console.log(data);
      var full = data.target.response;
    	$("#target").html($(full).find("#firstHeading"));
    };
    req.send();
	}
})
#target {
  border:1px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>This will request the title of the Wikipedia's page using the help of https://cors-anywhere.herokuapp.com</p>
<div id="target">
</div>

我真的不熟悉CORS,但我想基本的想法是目标服务器在发送数据之前必须同意来源是安全的。如果我错了,请纠正我。

使用dataType:"jsonp"的$.ajax()JQuery方法,您可以更新/添加带有其id的表。为此,您可以从需要数据的位置跨域访问站点。为此,您需要使用服务器端语言,如C#、PHP或其他从您想要数据的位置到站点的语言。如果你使用的是asp.net技术,那么你可以点击链接http://www.niceonecode.com/Q-A/JAVAScript/AJAX/Cross-domain-ajax-request-to-a-json-file-using-JSONP/20154