Undefined data

Par Amstramgram, il y a 10 ans


Bonsoir,

j'ai un petit soucis et je ne comprend vraiment pas pourquoi il me donne un undefined. Si quelqu'un peut m'expliquer vite fait.

Merci, des bisous/

getBookmarks(); function getBookmarks(data){ var data = getJSON(); console.log('getBookmarks:'+data); // ici undefined pourquoi ? } function getJSON(){ $.getJSON( 'http://127.0.0.1:8080/I-am-Groot/database/data.json', function(data){ console.log(data); // affiche bien les datas. } ); }

Merci.

6 réponses

cyyynthia, il y a 10 ans

Quel 'console.log' s'active en 1er ? 'Undefined' ou le résultat ?

cyyynthia, il y a 10 ans

Il faut que tu fasse un

return data;

dans ton getJson

Amstramgram, il y a 10 ans

J'ai bien rajouté le return dans getJSON. J'ai testé dans les deux cas cela me fais toujours un undefined.

function getJSON(){ $.getJSON( 'http://127.0.0.1:8080/I-am-Groot/database/data.json', function(data){ console.log(data); // affiche bien les datas. //return data; ici } ); //return data; et ici aussi }
ico, il y a 10 ans

Si je me goure pas c'est une histoire de asynchrone du coup faut utilise des promise

var getjson = $.getJSON( 'http://localhost/getjson/data.json', function (data) { return data; } ); function getBookmarks(){ getjson.done(function(p){ console.log(JSON.stringify(p)); }); } $(function () { getBookmarks(); });
cyyynthia, il y a 10 ans

@ico : exactement

Amstramgram, il y a 10 ans

Le 'undefined'.