jquery function - doesn't return
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text">How to return AJAX response Text?</a>  </p>
</blockquote>



<p>I have a bit of a problem with my getJsonObject function. 
What I want my code to do is everytime I click next, getJsonObject function gets the next page of articles and, when dom is ready, I display the headline of the article. But if there's an error(like when we run out of pages) then the pageNr won't increment anymore.
The problem is that getJsonObject function doesn't return 0 although the alert('return is 0)' does display.</p>

<p>Here is my code: </p>

<pre><code>&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;World News&lt;/title&gt;
        &lt;link rel="stylesheet" type="text/css" href="css/fullWindow.css" /&gt;
        &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;&lt;/script&gt;
        &lt;script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"&gt;&lt;/script&gt;
        &lt;script src = "https://raw.github.com/janl/mustache.js/master/mustache.js" type = "text/javascript"&gt;&lt;/script&gt;
    &lt;body&gt;      
        &lt;section id="mainContent"&gt;          
            &lt;div class="prevNext floatRight"&gt;&lt;a id="prev" href="#"&gt;prev&lt;/a&gt; | &lt;a id="next" href="#"&gt;next&lt;/a&gt;&lt;/div&gt;
            &lt;section id="news"&gt;
                &lt;p id="headline"&gt;This is my headline for the moment.&lt;/p&gt;

            &lt;/section&gt;
        &lt;/section&gt;              


    &lt;script type="text/javascript"&gt;
        $(document).ready(function(){
            var pageNr = 1;
            var sectionName = "science";
            var err = '2';

            $("#next").click(function(){

                /* it removes the tag 'p' with id="headline" and inserts some other content */
                $.when($('#headline').remove()).then(function(){                            
                    err = getJsonObject(sectionName, pageNr+1);
                    alert('err: '+err);

                    /* if there is no error go to the next page */
                    if(err == '0'){
                        pageNr++;                   
                    }
                });
            });
        });
        function getJsonObject(sectionName, pageNr){        

            $.getJSON('http://content.guardianapis.com/search?q=' + sectionName +      '&amp;page=' + pageNr + '&amp;page-size=5&amp;order-by=relevance&amp;format=json&amp;show-fields=all&amp;date-  id=date%2Flast7days&amp;api-key=gd6ndfndjyr8sd2p3rq3ubkg&amp;callback=?', 
            function(data){                 
                var results;    

                /* I get status == error when I finish iterating the pages*/    
                if(data.response.status != 'error'){            

                    $('#news').append('&lt;p id="headline"&gt;'+data.response.results[pageNr].fields.headline+'&lt;/p&gt;');                        

                    alert('return is 0');
                    return '0';
                }else{

                    alert('return is 1');           
                    return '1';         
                }
            });
        }
    &lt;/script&gt; 
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Any ideas why return doesn't work? Thanks.</p>
