2

I am getting a response from ajax request(done by jquery).

I have a method that displays errors to the users. Sometimes though I have some other information coming along with the json request.

So I don't want this info shown(where the rest of the errors are). So I figured since I always know the length of the json coming back I can just shorten the length since the error method just uses a while loop so if it is one less then it won't display that json part.

So I did this

var length = result.length -1;

ErrorsMethod(result, length); // call function.

   function ErrorsMethod(result, length)
    {
       while( i < length)
       { 
         // do stuff.
       }
    }

length is always undefined when it gets passed in though. I don't understand why.

I then tried

length.length ParseInt(length);

None seems to work. I don't even know what I am working with. When I do an alert of "length" var before it goes into the function it spits out a number.

1
  • 1
    Could you please show the code for the ajax request - and a sample return data from the ajax request? And how it all ties in with this function? I think your probably just not referencing the parameters correctly. Commented Sep 8, 2009 at 23:44

3 Answers 3

5

Were you correctly calling parseInt?

var length = parseInt(result.length) - 1;
ErrorsMethod(result, length);

ErrorsMethod(result, length); // call function.

   function ErrorsMethod(result, length)
    {
       while( i < length)
       { 
         // do stuff.
       }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Always supply the second parameter to parseInt, 10 in most cases. See stackoverflow.com/questions/850341
1
ErrorsMethod(result); // call function.

function ErrorsMethod(result)
{
    if (result && result.length)
    for ( var i = 0 ; i < result.length ; ++i )
    { 
        // do stuff.
    }
}

Comments

0

nor result or i are defined variables so thats probably why the script stops executing. try something like this:

<script type="text/javascript">
    var result = [10, 2];
    var length = result.length;


    ErrorsMethod(result, length); // call function.

    function ErrorsMethod(result, length)
    {
       var i=0;
       while( i < length)
       { 
         // do stuff.
         alert(i);
         i++;
       }
    }

</script>

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.