controller:
def get_nodes
...
render :text => nodes.to_json.to_s
end
which renders valid JSON ( Its picked up and parsed by my chrome plugin )
I’m using this in my JS:
var nodes = $.get('http://localhost:3000/users/get_nodes/4fb2739045a86e0c5c000002');
console.log(nodes);
console.log output: https://i.stack.imgur.com/2l7pX.png
I’m able to get the data successfully however all I want is the ‘responseText’ saved into the ‘nodes’ variable.
I’ve managed to isolate it to this code by replacing this dynamic var with a static one ( in that case it behaves as expected )
$.getJSON()and$.get()do not return the JSON or other response that was fetched. To retrieve that, you need to pass a success handler togetJSON()and it will get called AFTER the network operation is complete when the data is available.You also need to know that the function is normally asynchronous which means it returns immediately and some time later your success handler is called with the retrieved data.
See the
getJSONjquery doc for info.