camsong / fetch-jsonp

Make JSONP request like window.fetch
998 stars 158 forks source link

Uncaught ReferenceError: jsonCallback is not defined #49

Closed NavarroHoz closed 3 years ago

NavarroHoz commented 6 years ago

I try to consume an API that returns a function called jsonCallback, I put the following code and return the error

var fetchJsonp = require("fetch-jsonp") fetchJsonp("http://opendata.euskadi.eus/contenidos/ds_recursos_turisticos/albergues_de_euskadi/opendata/alojamientos.json", { jsonpCallback: 'jsonCallback', }) .then(function(response) { return response.json() }).then(function(json) { console.log('parsed json', json) }).catch(function(ex) { console.log('parsing failed', ex) })

If I look in the header of the request, I see that it happens as a parameter a random callback function.

captura

I'm probably doing something wrong,

Some solution or idea.

Thank you

kaidjohnson commented 6 years ago

Based on what I see when going to http://opendata.euskadi.eus/contenidos/ds_recursos_turisticos/albergues_de_euskadi/opendata/alojamientos.json, it appears that this service is always going to return the callback as jsonCallback. So the request you want to build is http://opendata.euskadi.eus/contenidos/ds_recursos_turisticos/albergues_de_euskadi/opendata/alojamientos.json?callback=jsonCallback which means you want to use:

fetchJsonp("http://opendata.euskadi.eus/contenidos/ds_recursos_turisticos/albergues_de_euskadi/opendata/alojamientos.json", { 
  jsonCallbackFunction: 'jsonCallback'
});

Check out https://github.com/camsong/fetch-jsonp#difference-between-jsonpcallback-and-jsoncallbackfunction for clarification between jsonpCallback and jsonCallbackFunction.

hanxzi commented 5 years ago

jsonpCallbackFunction: 'jsonCallback'