facebook和twitter iframe请求访问设置'document.domain'错误

时间:2013-01-10 21:48:10

标签: javascript facebook twitter

我正在将Tweet和Facebook Like按钮放在正在运行的项目上。一切似乎都在工作但是当点击btns我得到这个JS错误:

Unsafe JavaScript attempt to access frame with URL http://platform.twitter.com/widgets/tweet_button.1357735024.html#_=1357834238249&count=none&id=twitter-widget-0&lang=en&original_referer=http%3A%2F%2Fwww.sandals.com%2Fmain%2Fnegril%2Fne-home.cfm&size=m&text=Negril%2C%20Jamaica%20All%20Inclusive%20Vacation%20-%20Sandals%20Negril%20Beach%20Resort%20%26%20Spa&url=http%3A%2F%2Fwww.sandals.com%2Fmain%2Fnegril%2Fne-home.cfm&via=SandalsResorts from frame with URL http://www.facebook.com/plugins/like.php?locale=en_US&app_id=150389325070106&href=www.sandals.com/main/negril/ne-home.cfm&send=false&layout=button_count&width=60&show_faces=false&action=like&colorscheme=light&font&height=21. The frame requesting access set 'document.domain' to 'facebook.com', the frame being accessed set it to 'twitter.com'. Both must set 'document.domain' to the same value to allow access.

这是我用于fb和twitter的代码

window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId      : '150389325070106', // App ID from the App Dashboard
channelUrl : '//www.sandals.com', // Channel File for x-domain communication
status     : false, // check the login status upon init?
cookie     : true, // set sessions cookies to allow your server to access the session?
xfbml      : true,  // parse XFBML tags on this page?
frictionlessRequests: true
});
// Additional initialization code such as adding Event Listeners goes here
};

// Load the SDK's source Asynchronously
(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));


// Twitter Btn JS
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

我按照fb开发站点中的步骤进行了操作但仍然遇到了这些错误:S

1 个答案:

答案 0 :(得分:0)

正如您可以在链接的答案中看到的,这是Twitter的API的一个问题。作为关于链接问题的答案的替代,我创建了一个自定义推文按钮,看起来与Twitter的JS API完全相同,但不使用它,但仍然包括共享计数。随意使用它。

演示:http://fiddle.jshell.net/eyecatchup/Th6P2/2/show/
代码:https://github.com/eyecatchup/tweetbutton