How can I load content from another site onto mine with JavaScript/jQuery?

荒凉一梦 提交于 2020-01-05 06:37:14

问题


I'm trying to get a wikipedia article to load onto my site. I'm trying to follow the instructions here: http://en.wikipedia.org/wiki/Wikipedia:WikiProject_Transwiki but I'm at a loss.

I've tried:

var xyz = document.getElementById(url("http://en.wikipedia.org/w/index.php?title=Special:Export&history=1&action=submit&pages=Albert_einstein")

var xyz = $('#xyz').load('http://en.wikipedia.org/w/index.php?title=Special:Export&history=1&action=submit&pages=Albert_einstein');

document.write(xyz);

回答1:


If you are using a modern browser, you should be able to use CORS. According to the Wikipedia API docs, you need to pass an additionnal origin parameter that matches the Origin header sent by your browser.

http://en.wikipedia.org/w/api.php

"origin - When accessing the API using a cross-domain AJAX request (CORS), set this to the originating domain. This must match one of the origins in the Origin: header exactly, so it has to be set to something like http://en.wikipedia.org or https://meta.wikimedia.org . If this parameter does not match the Origin: header, a 403 response will be returned. If this parameter matches the Origin: header and the origin is whitelisted, an Access-Control-Allow-Origin header will be set."




回答2:


You can't load content from a different domain than your own through JavaScript. The JS security policy prevents it.

"In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number[1 – to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.[1]"
-- Wikipedia, from W3C

Shazbot suggests an iframe, but iframes are deprecated. Use objects :

<div class="timeContainer" style="background:#333; color:#090; padding:10px 0;">
  <div style="text-align:center; width:100%;">Current Date and Time</div> <!-- Heading, replaceable with hx tag -->

<!--[if IE]>
 <object classid="clsid:25336920-03F9-11CF-8FD0-00AA00686F13" data="your.url/" style="width:100%; height:19px;">
  <p>backup content</p>
 </object>
<![endif]-->

<!--[if !IE]> <-->
 <object type="text/html" data="your.url/" style="width:100%; height:19px;">
  <p>backup content</p>
 </object>
<!--> <![endif]-->
</div> <!-- timeContainer -->

Alternatively, you can use cURL (if your server supports it) through PHP. I'm not sure about Python, but I assume python can make use of cURL as well.



来源:https://stackoverflow.com/questions/15936288/how-can-i-load-content-from-another-site-onto-mine-with-javascript-jquery

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