ILIAS ILIAS - 6 months ago 26
Javascript Question

Pass a variable inside tags argument of Flickr API

Here is my working code (in Angular):

var url ="https://api.flickr.com/services/rest/?method=flickr.photos.search&
api_key=c4e2f731926eefa6fe1d3e9c2c9f9449&tags=coffee&format=json&jsoncallback=JSON_CALLBACK";

$http.jsonp(url).then(function(response) {
console.log(response.data);},function(){console.log('Error retrieving JSON data')}
);


However
I do not know how to send to
tags
a variable's value
and not just writing coffee or chocolate. Is there a better way to organize all this information (api_key, format, tags) in an object and append it to url?

Answer

You can use config.params object to set tags GET parameter:

var url = "https://api.flickr.com/services/rest/?api_key=c4e2f731926eefa6fe1d3e9c2c9f9449&method=flickr.photos.search&format=json&jsoncallback=JSON_CALLBACK";

$scope.tags = 'coffee';

$http.jsonp(url, {
  params: { tags: $scope.tags }
})
.then(function(response) {
  console.log(response.data);
}, function() {
  console.log('Error retrieving JSON data')
});
Comments