1

i want to access variables outside scope which is not working

$.getJSON("ajax_files/getSingleRow.php?id="+id+"&type="+type, function(json){ 
  var json = json[0];
  $.each(json, function (key, val) {
    //alert(key + '=' + val);
    $("#"+key).val(val);
  });
});

Now i want to use those variables here which is not working

alert(json.id);

Here is my json varible formation on getSingleRow.php

$results = array();

while($rw = $oAppl->row($res)) {
  $results[]=array(
    'id'=>$rw['id'],
    'ref'=>$rw['ref'],
    'name'=>$rw['name'],
    'description_demo'=>$rw['description_demo']
  );
}

echo json_encode($results);

[{"id":"1","ref":"RH-R-1","name":"","description_demo":"this is desc test"}]
5
  • Could you show your JSON content? You should not do var json = json because the variable json already exists. Commented Mar 4, 2013 at 19:32
  • i want to get it later,outside this code..how i can do this? Commented Mar 4, 2013 at 19:32
  • Could you do console.log(key, val) inside your each function? We could see if the JSON is OK. Commented Mar 4, 2013 at 19:33
  • [{"id":"1","ref":"RH-R-1","name":"","description_demo":"this is desc test"}] Commented Mar 4, 2013 at 19:35
  • It is from the php or in your getJSON call? Commented Mar 4, 2013 at 19:36

2 Answers 2

1

Because you are declaring the variable inside the callback.

Try

var json = null;
$.getJSON("ajax_files/getSingleRow.php?id="+id+"&type="+type, function(json){ 
  json = json[0];
  $.each(json, function (key, val) {
    //alert(key + '=' + val);
    $("#"+key).val(val);
  });
});

Furthermore, don't forget that the $.getJSON function is asynchronous. Therefore, you will not be able to use your variable until the server response.

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

1 Comment

Which part is not working? If alert show an empty result, it means that the server has not still returned the data. If the alert doesn't pop at all, it means that you have a JS script error. In this case, check the console log F12.
1

If you want to access a variable outside of the function scope, then you just need to define it outside and set that variable instead in your call to getJSON.

There is an issue with this approach. Since the function is a callback, it will execute when the request completes which will likely be after that alert gets called if it is simply line of code after your call to getJSON. So you will need to restrict that somehow. In the example below, there is a function called at the end of the callback's execution.

var externalJson;
$.getJSON("ajax_files/getSingleRow.php?id="+id+"&type="+type, function(json){
    externalJson = json[0];
    $.each(externalJson, function (key, val) {
         //alert(key + '=' + val);
         $("#"+key).val(val);
    });

    // Function used to gate attempt to access JSON content until it has been processed
    finishUp();
});

function finishUp() {
    alert(externalJson.id);
}

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.