0

I had a previous question where I was having issues for getting json from an url and storing it to a variable in order to pass it further to a function. I managed to get redirected to a solution that seemed promising, but it's still giving me undefined. The proposal was to use a callback as follows, but it doesn't seem to do the job.

var space = "";
function spaceCallback(data) {
  fplan = data.space['space'];
}
function getSpace() {
  var url = "http://localhost:8000/spaces/send_space/"
  $.getJSON(url, function (data) {
    spaceCallback(data);
  });
}

I would need to pass space to blueprint3d.model.loadSerialized(space);.

What am I missing here?

Daniel
  • 297
  • 3
  • 10
  • `console.log` `data` in `spaceCallback()` – Dev Man May 16 '20 at 14:52
  • That outputs the json I would like to pass to `blueprint3d.model.loadSerialized(space);` – Daniel May 16 '20 at 14:53
  • @Daniel Please put a `console.log(data)` inside your $.getJSON callback. It seems your server is not able to send JSON formatted data. Please check. – Rush W. May 16 '20 at 14:56
  • i meant what @Tethys0 is saying, seems your server is not returning anything, adding a `console.log` in `spaceCallback()` to check whether the value is reaching the function – Dev Man May 16 '20 at 15:41

1 Answers1

-1

Is it possible that you need to return your function?

function getSpace() {
  var url = "http://localhost:8000/spaces/send_space/"
  $.getJSON(url, function (data) {
   return spaceCallback(data);
  });
}

or ES6:

function getSpace() {
  var url = "http://localhost:8000/spaces/send_space/"
  $.getJSON(url, (data) => spaceCallback(data));
}
developerbase
  • 31
  • 1
  • 5