javascript - How to update background of page dynamically with Flickr API -


edit: static source urls can constructed flickr images. explanation: https://www.flickr.com/services/api/misc.urls.html


i making ajax request grab photos , parse information in order update background of page.

this rough code:

$.ajax({         url: 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=[api_key]&safe_search=&lat=' + geolat + '&lon=' + geolong + '&format=json&nojsoncallback=1',         error: function() {                  console.log('flickerapi error');                },         success: function(data) {           var photoid = data.photos.photo[0].id;           var ownerid = data.photos.photo[0].owner;           var backgroundimageurl = 'http://www.flickr.com/photos/' + ownerid + '/' + photoid + '/';            $('body').css('background-image','url(backgroundimageurl)');                },         always: function() {                 console.log('finished flicker');                }  }); 

when log console info, error: http://localhost:4567/backgroundimageurl 404 (not found).

but when log out backgroundimageurl get: http://www.flickr.com/photos/[the right photo id]/[the right user id]/ if follow link, takes me page want.

i've searched docs, , looks don't pass url attribute.

is problem using localhost? there better way update background image? thanks

you using string literal backgroundimageurl rather using contents of variable (the actual url). because string doesn't contain host or protocol thinks relative current request.

you need append value of variable when making url instead.

$('body').css('background-image','url(' + backgroundimageurl + ')'); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -