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>
Adarsh M Pallickal
  • 813
  • 3
  • 16
  • 37
  • `$.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 – Sachin Jain Dec 06 '13 at 04:29
  • Give me an idea for get values from this $.getJson – Adarsh M Pallickal Dec 06 '13 at 04:32
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Felix Kling Dec 06 '13 at 04:46

4 Answers4

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

  • Then how can I get this values? I need a solution not advice. – Adarsh M Pallickal Dec 06 '13 at 04:25
  • 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 –  Dec 06 '13 at 04:29
  • $.getJSON return some useless objects for show the function is ready. – Adarsh M Pallickal Dec 06 '13 at 04:30
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
    }
    })                              

});
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;
   });
});
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
0

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