function cbfunc(o){ var name = o[0].name; var id = o[0].id; if(id == undefined){ $('body').append('ERROR'); }else{ $.get('saveTodoist.php', {name: name, id: id}, function(){ window.location.reload(); }); } } $(document).ready(function(){ var token = ""; var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://todoist.com/API/getProjects?token='+token+'&format=json&callback=cbfunc'; document.getElementsByTagName('head')[0].appendChild(script); });