0

i getting the json response via ajax so when i doing alert of the json response array length is showing correctly the code is

try {
    xmlHttp.open("GET", "ajaxmodel_new.php?model_id="+model_id,true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status==200){
        model_details_json =  JSON.parse(xmlHttp.responseText)
        alert(model_details_json.communication.length)
        }
    }

so the alert is giving as 4 .... the same thing i.e so i need to use model_details_json.communication.length in other function say

function options(){

    document.getElementById('selected_opt').style.display = '';

    if(model_details_json.communication.length != 0 ){
}
}

it is showing error as model_details_json.communication is undefined , here model_details_json is an global varaible

1
  • call the function options() within the callback function of ajax. Commented Mar 29, 2011 at 13:56

3 Answers 3

1

If options() is being called before the AJAX Objects receives the response it is NULL and therefore you can't access.communication.length.

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

Comments

1

Are you making sure that you're only calling options when the response is ready? Since model_details_json is only defined in your if block after the response is ready, it will be undefined otherwise.

In the try block you're checking to make sure that the response is ready before checking the length of model_details_json.

Comments

0

Try declaring model_details_json somewhere outside of your functions so it's scope is global:

var model_details_json;

function options() {
...
}

That will make the variable available to both of your functions.

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.