2015-03-18 27 views
0

使用python -m SimpleHTTPServer 8080測試JS,我已經建立了一個D3即但是遇到問題用下面的代碼訪問instagram's api using oembed使用instagram的dembed與d3?

d3.json("http://api.instagram.com/oembed?url=https://instagram.com/p/rigacvhhTe/", function(error, json) { 
    if (error) return console.warn(error); 
    console.log(json); 
}); 

錯誤:

XMLHttpRequest cannot load http://api.instagram.com/oembed?url=https://instagram.com/p/rigacvhhTe/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. 

成功的響應應該返回這個以.json :

{ 
provider_url: "http://instagram.com/", 
media_id: "784331840172725470_179100388", 
author_name: "simplyclimb", 
height: null, 
thumbnail_url: "http://photos-d.ak.instagram.com/hphotos-ak-xpa1/t51.2885-15/10523538_897068253642371_552234973_n.jpg", 
thumbnail_width: 640, 
thumbnail_height: 640, 
provider_name: "Instagram", 
title: "Big day.", 
html: "<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-version="4" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"> <div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:50% 0; text-align:center; width:100%;"> <div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;"></div></div> <p style=" margin:8px 0 0 0; padding:0 4px;"> <a href="https://instagram.com/p/rigacvhhTe/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;" target="_top">Big day.</a></p> <p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">A photo posted by @simplyclimb on <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2014-08-11T01:14:23+00:00">Aug 10, 2014 at 6:14pm PDT</time></p></div></blockquote> <script async defer src="//platform.instagram.com/en_US/embeds.js"></script>", 
width: 658, 
version: "1.0", 
author_url: "http://instagram.com/simplyclimb", 
author_id: 179100388, 
type: "rich" 
} 

對於我在做什麼有什麼建議嗎?

+0

該API似乎不支持CORS,因此無法從客戶端腳本訪問它 – adeneo 2015-03-18 21:32:59

+0

然而,Instagram API確實支持JSONP,但它似乎並不像D3支持JSONP。 – adeneo 2015-03-18 21:36:10

+0

@adeneo使用d3的JSONP? http://bl.ocks.org/tmcw/4494715 – blehman 2015-03-18 21:40:01

回答

0

我嘗試使用this plugin爲JSONP格式,但照片沒有呈現(我正在運行Chrome),只顯示標題。

d3.jsonp = function (url, callback) { 
    function rand() { 
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 
     c = '', i = -1; 
    while (++i < 15) c += chars.charAt(Math.floor(Math.random() * 52)); 
    return c; 
    } 

    function create(url) { 
    var e = url.match(/callback=d3.jsonp.(\w+)/), 
     c = e ? e[1] : rand(); 
    d3.jsonp[c] = function(data) { 
     callback(data); 
     delete d3.jsonp[c]; 
     script.remove(); 
    }; 
    return 'd3.jsonp.' + c; 
    } 

    var cb = create(url), 
     script = d3.select('head') 
     .append('script') 
     .attr('type', 'text/javascript') 
     .attr('src', url.replace(/(\{|%7B)callback(\}|%7D)/, cb)); 
}; 

function wtf_jsonp(instaG) { 
    d3.select('.photo').remove(); 

    d3.select("body") 
    .append('div') 
    .attr('id','photo') 
    .html(instaG.html); 
}; 

var circle = d3.select('svg') 
.append('circle') 
.attr("cx",100) 
.attr("cy",50) 
.attr("r",30) 
.attr("fill","red") 
.attr("stroke","black"); 

circle.on("mouseover",function(event){ 
    d3.jsonp("http://api.instagram.com/oembed?url=" + "https://instagram.com/p/rigacvhhTe/" + "&callback=wtf_jsonp"); 
}); 

見下面錯誤:enter image description here

作爲替代方案,我只是用的shortcode僅呈現圖像。

var circle = d3.select('svg') 
.append('circle') 
.attr("cx",100) 
.attr("cy",50) 
.attr("r",30) 
.attr("fill","red") 
.attr("stroke","black"); 

circle.on("mouseover",function(event){ 
    d3.select('body').append('img') 
     .attr('src','http://instagram.com/p/' + "rigacvhhTe" +'/media/?size=l'); 
}); 

見成功繪製如下: enter image description here 該解決方案爲我的目的。

任何想法我與d3.jsonp()電話打了什麼錯誤?