Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get remote JSON without Jquery?

What I want to do specifically is get the Vimeo thumbnail image by grabbing the json data returned by a request. I can't use jquery due to the javascript file being a small remote file that the user calls and jquery would increase its size many many times.

I have looked and everything seems to be talking about jquery or getting it in another language (php for instance).

I have found I need to do this so far:

var script = document.createElement('script');
script.src = theUrlToMakeTheRequest;
document.getElementsByTagName('head')[0].appendChild(script);

I am not sure what I need to make the callback and not sure what I need to do to be able to use the url that gets appended.

like image 206
qitch Avatar asked Mar 10 '12 19:03

qitch


2 Answers

From vimeo's documentation

http://vimeo.com/api/docs/simple-api

It looks like you can put a ?callback=myfunction parameter on the end of the url to do a jsonp type of callback. So your code would maybe look something like this.

function myfunction(data) {
    alert(data);
}

var script = document.createElement('script');
script.src = theUrlToMakeTheRequest + '?callback=myfunction';
document.getElementsByTagName('head')[0].appendChild(script);

Their downloads page looks to have examples for just what you are trying to do. http://vimeo.com/api/docs/downloads

like image 147
Al W Avatar answered Oct 19 '22 11:10

Al W


You can do it with a raw XMLHttpRequest,

Or for a small lightweight library that can do it check out zepto.js

If you just include zepto.js and ajax.js from... https://github.com/madrobby/zepto/tree/master/src

You would have a JQuery compatible solution in 30k uncompressed javascript (probably < 4k minified)

like image 31
danmux Avatar answered Oct 19 '22 09:10

danmux