2

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.sstatic.net/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 )

1 Answer 1

4

$.getJSON() and $.get() do not return the JSON or other response that was fetched. To retrieve that, you need to pass a success handler to getJSON() 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 getJSON jquery doc for info.

$.getJSON('http://localhost:3000/users/get_nodes/4fb2739045a86e0c5c000002', function(data) {
    // you can access your data here in the "data" variable passed to this success handler
    console.log(data);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.