如何对URL进行JSON调用? [英] How to make a JSON call to an URL?
本文介绍了如何对URL进行JSON调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在查看以下API:
I'm looking at the following 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对象?
What do I have to do to get this JSON object from just an URL?
推荐答案
似乎他们为format参数提供了 js
选项,该选项将返回JSONP.您可以像这样检索JSONP:
It seems they offer a js
option for the format parameter, which will return JSONP. You can retrieve JSONP like so:
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);
});
这篇关于如何对URL进行JSON调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文