Javascript Flickr API帮助程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript Flickr API帮助程序相关的知识,希望对你有一定的参考价值。
Class to create Flickr-API REST calls.
/* *********************************************************************** * Class to create Flickr-API REST calls. * See http://www.flickr.com/services/api/ for supported * methods, params and returns. * 1. You need a valid API-Key * (http://www.flickr.com/services/apps/create/apply/) * 2. This class is supported on * - Internet Explorer 8+ * - Firefox 3.1+ * - Safari 4+ * - Chrome 3+ * - Opera 10.5+ * If you're using an older browser, include the json2.js library * (https://github.com/douglascrockford/JSON-js/blob/master/json2.js) * 3. This class only supports JSON-response-format * 4. This class doesn's support authentication and methods which * require user authorization. * * CONSTRUCTOR(apiKey): * Parameters: apiKey: * Your valid API key * * METHODS: * Api(method, callback, params, cparams): * Creates a async request for given method with given params. * Parameters: method: * Name of the Flickr-API-REST method as STRING. * callback: * Function to call after response. Should take minimum * one parameter as API-Response as JS-Object, * 2. optional parameter = cparams (callback-params) * params: * API-method parameters as Object * cparams (optional). Additional parameters for call- * back function. * Return: VOID * * Api_S(method, callback) * Creates a sync request for given method with given params. * Parameters: See Flickr.Api-method * Return: API-Response as JS-Object. * * //////////////// EXAMPLE ///////////////// * var f = new Flickr("[MyApiKey]"); * f.Api( * "flickr.photos.search", * function(resp){ * if(resp.stat != "fail"){ * alert("Number of photos: " + resp.photos.total);} * }else{ * alert("API-error: " + resp.message); * } * }, * {user_id: "[MyFlickrUserId]"} * ); * * **********************************************************************/ function Flickr(apiKey){ if(!apiKey){throw "InvalidApiKeyProvided";} this.key = apiKey; this.Api = function(method, callback, params, cparams){ var xhttp = new XMLHttpRequest(); xhttp.open('GET', this.createUrl(method, params), true); xhttp.onreadystatechange = function(){ if(xhttp.readyState == 4){ callback(JSON.parse(xhttp.responseText), cparams); } } xhttp.send(null); } this.Api_S = function(method, params){ var xhttp = new XMLHttpRequest(); xhttp.open('GET', this.createUrl(method, params), false); xhttp.send(null); return JSON.parse(xhttp.responseText); } this.createUrl = function(method, params){ var url = "http://api.flickr.com/services/rest/?method=" + method + "&api_key=" + this.key + "&format=json" + "&nojsoncallback=1"; if(params){ for(var p in params){ url += "&" + p + "=" + params[p]; } } return url; } }
以上是关于Javascript Flickr API帮助程序的主要内容,如果未能解决你的问题,请参考以下文章
javascript 用于CORS请求的jQuery.ajax()方法的Flickr API请求
ios flickr api:未调用 Flickr 回调 URL