Help, I'm confused.
I just need to get a JSON object into my page. The URL is as follows:http://disqus.com/api/get_forum_posts/?user_api_key=MYKEY&forum_id=MYID&api_version=1.1
If I use the Flickr API URL and the code given in the getJSON example, it works fine:
<script>$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
function(data){
alert(data);
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});</script>
But, if I substitute in the Disqus URL above, the data is null. After reading JSON Feed Returning null while using jQuery getJSON I checked whether this URL returned valid JSON, and it does.
The Disqus API seems to suggest i开发者_运维百科t supports JSONP - 'You can also pass "jsonp:callback" as the "api_response_format" parameter'. I've tried adding this parameter, but it doesn't help.
What am I doing wrong? I suspect the problem may be that I don't really understand the difference between JSON and JSONP, and what I need to be doing to get JSONP back.
You can call it like this:
$.ajax({
url: 'http://disqus.com/api/get_forum_posts/?user_api_key=MYKEY&forum_id=MYID&api_version=1.1&api_response_format=jsonp:myFunction',
dataType: 'jsonp'
});
function myFunction(data) { //make sure this is available globally
//use data
}
You can give it a try here.
Like your documentation link, the parameter on the end api_response_format=jsonp:myFunction
is the key, check the link directly here, even though it's an error because of the key, see how for format is different? That's what's required for a JSONP call, the functionName({data})
response format.
Copying the two URL's provided by yourself into a browser window, the DISQUS URL returns with an error, "Problem loading page".
The Disqus call is missing a callback function
The api_response_format=jsonp:callback
argument is supposed to be the name of a callback function defined in by your Javascript. So let's say I have a function called handleJson
which is supposed to parse a JSON response, then you have to pass the argument jsonp:handleJson
. Disqus will then pass you code wrapped in a call to handleJson
.
What JSONP brings is the ability to get around the same-domain origin policy. Suppose that a URL <http://foo/json
> responds with this JSON object:
{
id: 2,
first_name: 'John',
last_name: 'Doe',
}
The only way you could use that object is if you are also on domain foo. But if you are not able to be on that domain, suppose that you are accessing an API that returns JSON format objects, then you ned a little help from that API provider.
Suppose that the url you request now becomes <http://foo/json?callback=handleJson
> and the API provider responds with this:
handleJson({id: 2, first_name: 'John', last_name: 'Doe'})
Now, the browser is going to try to run that snippet of Javascript inside of the environment you've defined. So if you've defined a handleJson
function, it's going to be called.
That's what JSONP is, a way to circumvent the same-domain origin policy and give you cross-site scripting with JSON objects.
Also, if your dates aren't padded with zeros, then the events won't show. Ex. 2010-9-5 bad Ex. 2010-09-05 good
精彩评论