I need to create a project in order to get into the JS boot disk I am accessing. They tell me that I can only use vanilla JS, in particular that frameworks and jQuery are not allowed. Until this moment, when I wanted to get the JSON file from the api, I would say
$.getJSON(url, functionToPassJsonFileTo)
for json calls and
$.getJSON(url + "&callback?", functionToPassJsonPFileTo)
for JSONP calls. I just started programming this month, so please remember that I donβt know the difference between JSON or JSONP or how they relate to this thing called ajax. Please explain how I will get what reached 2 lines in Vanilla Javascript. Thanks.
So, to clarify,
function jsonp(uri){ return new Promise(function(resolve, reject){ var id = '_' + Math.round(10000 * Math.random()) var callbackName = 'jsonp_callback_' + id window[callbackName] = function(data){ delete window[callbackName] var ele = document.getElementById(id) ele.parentNode.removeChild(ele) resolve(data) } var src = uri + '&callback=' + callbackName var script = document.createElement('script') script.src = src script.id = id script.addEventListener('error', reject) (document.getElementsByTagName('head')[0] || document.body || document.documentElement).appendChild(script) }) }
what is the equivalent of jsonp?
source share