小编典典

如何对URL进行JSON调用?

json

我正在查看以下API:

http://wiki.github.com/soundcloud/api/oembed-
api

他们举的例子是

呼叫:

http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=json

响应:

{
"html":"<object height=\"81\" ... ",
"user":"Forss",
"permalink":"http:\/\/soundcloud.com\/forss\/flickermood",
"title":"Flickermood",
"type":"rich",
"provider_url":"http:\/\/soundcloud.com",
"description":"From the Soulhack album...",
"version":1.0,
"user_permalink_url":"http:\/\/soundcloud.com\/forss",
"height":81,
"provider_name":"Soundcloud",
"width":0
}

我该怎么做才能仅通过URL获得此JSON对象?


阅读 382

收藏
2020-07-27

共1个答案

小编典典

看来他们js为format参数提供了一个选项,它将返回JSONP。您可以像这样检索JSONP:

function getJSONP(url, success) {

    var ud = '_' + +new Date,
        script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0] 
               || document.documentElement;

    window[ud] = function(data) {
        head.removeChild(script);
        success && success(data);
    };

    script.src = url.replace('callback=?', 'callback=' + ud);
    head.appendChild(script);

}

getJSONP('http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=js&callback=?', function(data){
    console.log(data);
});
2020-07-27