我正在尝试在我的网站上运行Twitter Feed。但是每当我加载页面时,都会出现以下错误:
XMLHttpRequest cannot load http://www.jciadt.com/demos/twitter/get_tweets.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jciadt.com' is therefore not allowed access.
有问题的页面可以在这里找到 - > http://jciadt.com/demos/twitter/testindex.html
我不知道为什么会这样。任何帮助将不胜感激。
<?php
session_start();
require_once('http://jciadt.com/demos/twitter/twitteroauth/twitteroauth/twitteroauth.php');
$twitteruser = "jackcoldrick";
$notweets = 30;
$consumerkey="xxxxxxx";
$consumersecret="xxxxxxx";
$accesstoken="xxxxxxx-xxxxxxx";
$accesstokensecret="xxxxxxx";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret){
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echo json_encode($tweets);
?>
答案 0 :(得分:0)
您正在尝试向不允许该资源的资源发出跨域请求。如果您从this link运行脚本,会发生什么?地址中的www
会将您置于不同的子域,并会干扰XHR。当我通过www
前导链接尝试时,我从脚本中得到500错误,而不是XHR失败错误。