1

I had a json file results.json Which shown below. And I had a html file contain some script. This is for retrieve data data. When I am enter into the html page which call a script function get_machFollow(que_script) this function is for receive json file data. The function is works fine and which alert correct output, But after this function return some data to my HTML page.

My JSON file

{"mach_fol_4": {"match_l":
       ["7","8","99"],"attempts":"0","feedback_true":"You are right!",
  "feedback_false":"Sorry! wrong answer."}}

This is my script function. This function is works fine but I can't alert the return value from HTML page. That shows undefined.

function get_machFollow(que_script)
{

        var return_var;
      $.getJSON('results.json', function(data) {
            return_var=data[que_script].match_r;    
            alert(return_var);//Working alert show correct output 
            return return_var;     
                 });

}

This is my html file

   <html>
    <head>
      <script type='text/javascript' src='js/jquery.min.js'></script>
      <script>
         $(document).ready(function(){
             var mach_follow_js;
             mach_follow_js=get_machFollow('mach_fol_4');
             alert(mach_follow_js);//Wrong output
         });
   </head>
    <body>
      <p>Hello world</p>
    </body>
    </html>
3
  • $.getJson is an asynchronous call so when you call get_matchFollow and try to alert the response in next line, response from ajax has not come.That's why it is coming as undefined Commented Dec 6, 2013 at 4:29
  • Give me an idea for get values from this $.getJson Commented Dec 6, 2013 at 4:32
  • possible duplicate of How to return the response from an AJAX call? Commented Dec 6, 2013 at 4:46

4 Answers 4

1

are you intending return return_var; to be inside the get_machFollow scope, because right now its inside the jquery function scope and will not return value to the main page

Sign up to request clarification or add additional context in comments.

3 Comments

Then how can I get this values? I need a solution not advice.
I have not tried this with jquery but check what the $.getJSON method is returning if its not void, you might be able to assign return_var= $.getJSON..., but keep the return in the getJSON and add a new one to the outer function
$.getJSON return some useless objects for show the function is ready.
1

Here below JSON data fetched by AJAX. It passing JSON Data Object in Alert. You can use it as you want. also can Iterate data using for loop or $.each function.

$(document).ready(function(){
    var mach_follow_js;
    // mach_follow_js=get_machFollow('mach_fol_4');

    //JSON Data Fetched by AJAX
    $.ajax('results.json',{
        type:'GET',
        dataType: "json",
        jsonCallback: 'successCallback',               
        async: true,
        beforeSend: function () {
        //if you want to show loader
    },
    complete: function () {
        //hide loader after download data
    },
    success: function (resultJSON) {                                       
        mach_follow_js = resultJSON;        // assigning to Global variable ProductResult                   
        alert(mach_follow_js);
        console.log(mach_follow_js);
    },
    error: function (request, error) {
        alert('Network error has occurred please try again!');//error
    }
    })                              

});

Comments

0

There are multiple ways by which you can do it. One of them is pass a callback handler to your method which will be called when you get the response. Try this:

function get_machFollow(que_script, sCallback)
{
      var return_var;
      $.getJSON('results.json', function(data) {
            return_var=data[que_script].match_r;    
            alert(return_var);//Working alert show correct output 
            sCallback.call(null /* context */, return_var);      
       });

}

$(document).ready(function(){
    var mach_follow_js;
    get_machFollow('mach_fol_4', function(output) {
        alert(output);
        match_follow_js = output;
   });
});

Comments

0

Use ajax callback function $.getJSON() is actually an ajax function. So you need to apply callback to perform this action.

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.