A: 

You need to append your values within the callback function. Right now, your the line of code after the two $.getJSON calls is firing before the JSON is finished downloading. That's why the first append is working. You have a timing issue.

To illustrate the timing, use alert messages like this...

$.getJSON('http://twitter.com/followers/ids.json?screen_name='+ query1 + '&callback=?', function(data) {
    f1=data;
    alert('Two');
    $('#content').append('p'+f1[0]+'p');//this one is coming                
});

$.getJSON('http://twitter.com/followers/ids.json?screen_name='+ query2 + '&callback=?', function(data1) {
    f2=data1;
    alert('Three');
});

alert('One');
$('#content').append('p'+f1[0]+'p');//this one is not coming...its showing Undefined
Josh Stodola
oh so i have to give time to both $.getJSON functions before i can append them.so how do i do tht?
anand
You have to utilize the callback structure of jQuery.
Josh Stodola
A: 

You never reference f2, so you may have a copy/paste bug. Did you mean to reference f1 again?

By the way, it's better to initialize the arrays like this

var f1=[];
var f2=[];
var common=[];

rather than using "new Array".

Also, as Josh pointed out, Ajax calls are asynchronous. You don't want to rely on the data coming in until it comes in.

BOTH of your $getJSON calls are going to come back immediately, so your program flow is not what you seem to expect.

jQuery's $getJSON takes a callback function as its third parameter. You want to set one up to do any processing after the call.

When you program with Ajax, you don't have one big piece of JavaScript, you have a bunch of event-driven pieces of code.

Nosredna
That *is* how he's initializing. I think you meant []
Josh Stodola
Yeah. I meant to edit that to [].
Nosredna
u dint understand the question. the first append is working because it is inside the $.getJSON but the other append is not since it is outside the $.getJSON function.why is that happeningand i meant to reference f1 again cause its jus a testing code.
anand
so il initialize with new array[];
anand
It's happening because Ajax calls are asynchronous.
Nosredna
+1  A: 

@anand, $.getJSON() retrieves JSON data in an asynchronous manner. The purpose of you callback functions is to perform work once the JSON has been received from the asynchronous request. I'll simplify your example some:

var query1 = urlencode($('input[name="searchTerm1"]').val()); //user1
var query2 = urlencode($('input[name="searchTerm2"]').val()); //user1
var rpp = 20; //number of tweets to retrieve out
var c=0;
var f1=new Array();
var f2=new Array();
var common=new Array();

$.getJSON('http://twitter.com/followers/ids.json?screen_name='+ query1 + '&callback=?', 
    // 1st callback
    function(data) {
        f1=data;

        // We know that f1 has been assigned, so unless the Twitter API
        // returns an empty Array, f1[0] will not be Undefined.
        $('#content').append('p'+f1[0]+'p');//this one is coming
});
$.getJSON('http://twitter.com/followers/ids.json?screen_name='+ query2 + '&callback=?', 
    // 2nd callback
    function(data1) {
        f2=data1;
});

// This statement may be executed before 1st callback and therefore
// f1 may still be an empty Array causing f1[0] to return Undefined.
$('#content').append('p'+f1[0]+'p');//this one is not coming...its showing Undefined

Please check out the comments regarding your calls to append(). Hope this helps!

Phil Klein
so wat il do is after both callback il set a time frame after which the 2nd append will be executed which gives $.getJSON ample time to get all the data from url..
anand
@anand, I wouldn't set a timeout. You're just asking for problems that way. You can never guarantee the response time for an asynchronous call. Instead you could nest the second $.getJSON call in the 1st callback. If doing so isn't clear, let me know.
Phil Klein
You need to use a callback. If you simply do a delay, the delay won't even start until this JavaScript routine exits anyway (JavaScript is single-threaded). You could do it as the callback in a timeout, but if you're going to do that, why not just use th callback facility of $getJSON?
Nosredna
k il nest it and see what happens....
anand