简体   繁体   English

Twitter o嵌入跨域请求被阻止

[英]Twitter oEmbed cross-origin request blocked

I'm trying to use the Twitter oEmbed API to make a request for an embedded tweet. 我正在尝试使用Twitter oEmbed API发出对嵌入式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.com/1/statuses/oembed.json?id=507185938620219395", true);
xmlhttp.send();

I'm getting a Cross-Origin Request Blocked error in Firefox. 我在Firefox中收到跨域请求阻止错误。 What am I doing wrong? 我究竟做错了什么?

The answer is "use jsonp" 答案是“使用jsonp”

$.ajax({
  type:     "GET",
  url:      "http://api.twitter.com/1/statuses/oembed.json?id=507185938620219395",
  dataType: "jsonp",
  success: function(data){
    console.log(data);
  }
});

https://ctrlq.org/code/19933-embed-tweet-with-javascript https://ctrlq.org/code/19933-embed-tweet-with-javascript

This one is a workaround for fixing the error 这是一种修复错误的解决方法

 <!-- Written by Amit Agarwal amit@labnol.org 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.com/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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM