April 25, 2012

Why isn't data being returned to the getJSON success handler?

Question by typoknig

I was calling getJSON and looping through the results like this and everything was working great:

$.getJSON( base_url + 'search/drill_down',{
        nextSelectName: nextSelectName,
        thisSelectName: thisSelectName,
        itemId: itemId
    }, function( r ) {
        $.each( r.items, function( k, v ) {
            //do stuff
        });
});

Now on a certain condition I need to return some HTML data, but I still need JSON for my original condition, so I figured I would just encode my HTML as JSON, but the data never returns! The original code still works fine, it is only when I’m trying to return HTML encoded as JSON that things blow up.

$.getJSON( base_url + 'search/drill_down',{
        nextSelectName: nextSelectName,
        thisSelectName: thisSelectName,
        itemId: itemId
    }, function( r ) {

        alert('working!'); // Not hitting this!

        if(r.tabs){
            $.each( r.tabs, function( k, v ) {
            var html = v[ 'html' ];
                return $('#content').html(html); // No need to continue, there is only one HTML string and no items.
            });
        }

        $.each( r.items, function( k, v ) { 
            //do stuff
        });
});

I have a break point on the server (using CodeIgniter) and everything there looks good. I checked checked the HTML I encoded as JSON and it is valid JSON. Why is the JSON generated by my server not making it back to the getJSON success handler?

Answer by Starx

The only explanation is the request was flaud, and reason might be many. One might be requesting on a wrong place. Check using

console.log(base_url + 'search/drill_down');

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!