2

Am new to jquery.I need to know how to pass external json file as a argument to a function.

I know how to pass as a object in client side like below.

var json = { "key": "value" }

Basically i need to call this below function by passing the json data.

function draw(json) {
 // Code here

  console.log(json);

}

In client side json means i know to how to pass.

draw(json);

But my json file is come from server(External Json File)

I tried jquery.getJson() method also.But not getting exact solution.I don't know what i did mistake also.

My $.getJson code

draw("$.getJSON("http://localhost/cb/json/Json_30647.json",function(data) {

      return data;   

    }));

Please help me to solve this issue.Thanks in advance.

5
  • 1
    You can not return from an asynchronous method! Commented Feb 2, 2015 at 19:55
  • blog.slaks.net/2015-01-04/async-method-patterns Commented Feb 2, 2015 at 19:55
  • Hi @epascarello.Thanks for reply.Then what is the way to achieve the solution.Please tell any idea.Am new to javascript. Commented Feb 2, 2015 at 19:57
  • 1
    So use instead: $.getJSON(url, draw); Commented Feb 2, 2015 at 19:57
  • stackoverflow.com/questions/14220321/… Commented Feb 2, 2015 at 19:57

1 Answer 1

4

You can not return from an asynchronous method. You are trying to eat the pizza before it it delivered to your house.

Call the function when the call is complete:

$.getJSON("http://localhost/cb/json/Json_30647.json",function(data) {
      draw(data);   
});
Sign up to request clarification or add additional context in comments.

2 Comments

HI @epascarello.Got a solution..Thank you so much.
Love the pizza delivery analogy!

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.