admin管理员组文章数量:1431750
I'm trying to use the Twitter oEmbed API to make a request for an embedded tweet. The code is super-simple right now:
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", ".json?id=507185938620219395", true);
xmlhttp.send();
I'm getting a Cross-Origin Request Blocked error in Firefox. What am I doing wrong?
I'm trying to use the Twitter oEmbed API to make a request for an embedded tweet. The code is super-simple right now:
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "https://api.twitter./1/statuses/oembed.json?id=507185938620219395", true);
xmlhttp.send();
I'm getting a Cross-Origin Request Blocked error in Firefox. What am I doing wrong?
Share Improve this question asked Mar 11, 2015 at 16:36 puzzlpuzzl 8319 silver badges21 bronze badges 3-
This will most likely work!
http://kylewbanks./blog/Fetching-Cross-Domain-JSON-Tweets-with-JavaScript-and-JSONP
– Timothy Dalton Commented May 5, 2016 at 21:32 - @TimothyDalton link is dead. – subdavis Commented Apr 2, 2017 at 15:55
-
@suBDavis try this
https://www.google./search?q=Fetching-Cross-Domain-JSON-Tweets-with-JavaScript-and-JSONP
, it's the first hit – Timothy Dalton Commented Apr 15, 2017 at 6:38
2 Answers
Reset to default 5The answer is "use jsonp"
$.ajax({
type: "GET",
url: "http://api.twitter./1/statuses/oembed.json?id=507185938620219395",
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
https://ctrlq/code/19933-embed-tweet-with-javascript
This one is a workaround for fixing the error
<!--
Written by Amit Agarwal [email protected]
Paste this anywhere between the body tag
-->
<style>
#tweet {
width: 400px !important;
}
#tweet iframe {
border: none !important;
box-shadow: none !important;
}
</style>
<div id="tweet" tweetID="515490786800963584"></div>
<script sync src="https://platform.twitter./widgets.js"></script>
<script>
window.onload = (function(){
var tweet = document.getElementById("tweet");
var id = tweet.getAttribute("tweetID");
twttr.widgets.createTweet(
id, tweet,
{
conversation : 'none', // or all
cards : 'hidden', // or visible
linkColor : '#cc0000', // default is blue
theme : 'light' // or dark
})
.then (function (el) {
el.contentDocument.querySelector(".footer").style.display = "none";
});
});
</script>
本文标签: javascriptTwitter oEmbed crossorigin request blockedStack Overflow
版权声明:本文标题:javascript - Twitter oEmbed cross-origin request blocked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745589124a2665091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论