tags:

views:

280

answers:

2

$(document).ready(function() {                      
    $('form#search').bind("submit", function(e){                            
            e.preventDefault();
            $('#content').html('');

// Define the callback function
  function getGeo(jsonData) {     
     $('#content').append('

'+jsonData.rank+'

'); bObj.removeScriptTag(); } // The web service call var req = 'http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=getGeo'; // Create a new request object bObj = new JSONscriptRequest(req); // Build the dynamic script tag bObj.buildScriptTag(); // Add the script tag to the page bObj.addScriptTag(); }); });

Im tryin to use the cross domain json request to get the json data which is


{
"user_id":"3190399",
"user_name":"Anand_Dasgupta",
"followers_current":"86",
"date_updated":"2009-06-04",
"url":"",
"avatar":"205659924\/DSC09920_normal.JPG",
"follow_days":"0","started_followers":"86",
"growth_since":0,
"average_growth":"0",
"tomorrow":"86",
"next_month":"86",
"followers_yesterday":"86",
"rank":176184,
"followers_2w_ago":null,
"growth_since_2w":86,
"average_growth_2w":"6",
"tomorrow_2w":"92",
"next_month_2w":"266",
"followersperdate":[]
}

im getting the json data from the url

http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=getGeo

But this code doesnot seem to work. If anyone could polish the code somehow or provide any response it will be highly appreciated. Thank You

+2  A: 

Just guessing here, but could it be that the getGeo function is out of scope when the jsonp callback is triggered? Maybe try moving the getGeo function out of your $(document).ready() block?

edit: alternatively, you're already using jQuery, right? jQuery will do the cross-domain stuff for you!

$(document).ready(function()
{
    $.getJSON('http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=?',function(jsonData){
        $('#content').append(' 
'+jsonData.rank+'
');
    }); 
});
Stobor
I agree. That is, in my opinion, the error.
Boldewyn
i tried using getJSON before and have failed.It just doesnt seem to show anything. Il try moving getGeo function and check.
anand
i moved getGeo out of $(document) but dint work.also i dont think twittercounter.com supports jsonp format.Any advice?
anand
+2  A: 

Stobor is on the right track. I visited the page with the class and how-to information that you obviously used: http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html. The script there takes advantage of the callback= value that Yahoo uses to specify the callback function that wraps the JSON data (thus making it JSONP data). You have a callback=getGeo in your URL, but the TwitterCounter API does NOT have a way to specify a callback function. I created a full HTML page using the code you were using:

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Twittercounter API Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="jsr_class.js"></script>
    <script type="text/javascript">
    var bObj;

    // Define the callback function
    function getGeo(jsonData) {     
     $('#content').append(''+jsonData.rank+'');
     bObj.removeScriptTag(); 
    }

    $(document).ready(function() {                      
        $('form#search').bind("submit", function(e){                            
                e.preventDefault();
                $('#content').html('');
                // The web service call
                var req  = 'http://twittercounter.com/api/?username=Anand_Dasgupta&amp;output=json&amp;results=3&amp;callback=getGeo'; 

                // Create a new request object
                bObj = new JSONscriptRequest(req); 

                // Build the dynamic script tag
                bObj.buildScriptTag(); 

                // Add the script tag to the page
                bObj.addScriptTag();
        });
    });
    </script>
    </head>

    <body>
    <form id="search">
    <input type="submit" id="search" value="Get Info" />
    </form>
    <div id="content">
    </div>
    </body>
    </html>

and Firebug gave me an error when I activated the button. The reason is based on this paragraph in the original article's writeup:

That's a valid JavaScript statement, so it can be the target of a script tag that returns JavaScript (raw JSON data, without the callback function, is not a valid JavaScript statement, so it will fail to load if it is the target of a script tag). For comparison, look at the XML version of this call here.

The "valid JavaScript statement" is the one with the function name wrapping the actual data.

Stobor's solution would be perfect, if Twittercounter would allow for JSONP requests and let you specify a wrapper function. As it is, you'll have to create your own proxy to act as an intermediary. I have an example on how to create one using PHP on my blog.

Tony Miller
yes your absolutely right. Twitter Counter doesnt allow a callback wrapper function.Kool i will go thru that then.BTW,i could also use xml in that case if i want to i guess.
anand
Thanks for taking the time to check the API docs themselves; I just cued off the fact that there was a "callback=" in the url, and went with that...
Stobor