Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:57:34+00:00 2026-06-16T19:57:34+00:00

Possible Duplicate: How to return AJAX response Text? I have a bit of a

  • 0

Possible Duplicate:
How to return AJAX response Text?

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.

Here is my code:

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

            </section>
        </section>              


    <script type="text/javascript">
        $(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 +      '&page=' + pageNr + '&page-size=5&order-by=relevance&format=json&show-fields=all&date-  id=date%2Flast7days&api-key=gd6ndfndjyr8sd2p3rq3ubkg&callback=?', 
            function(data){                 
                var results;    

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

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

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

                    alert('return is 1');           
                    return '1';         
                }
            });
        }
    </script> 
    </body>
</html>

Any ideas why return doesn’t work? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:57:35+00:00Added an answer on June 16, 2026 at 7:57 pm

    You should rewrite your getJsonObject with a callback function:

    function getJsonObject(sectionName, pageNr, callback){   
       $.getJSON(..., function(data) {
    
          ...
          callback('1');
       }
    

    Using getJsonObject:

    getJsonObject(sectionName, pageNr+1, function(err) {
       alert(err);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery ajax return value I have this jQuery script: $('#headerSubmit').click(function() { var
Possible Duplicate: return AJAX callback return I have made a JQuery code using AJAX,
Possible Duplicate: How to return AJAX response Text? I'm calling a javascript method which
Possible Duplicate: jquery ajax return: undefined I have written the following code to check
Possible Duplicate: How to return AJAX response Text? I'm trying to get the following
Possible Duplicate: jQuery: Return data after ajax call success I have the following function
Possible Duplicate: How to return AJAX response Text? I'm trying to put together a
Possible Duplicate: How to return AJAX response Text? How to return the response from
Possible Duplicate: How to return the response from an AJAX call from a function?
Possible Duplicate: How to return the response from an AJAX call from a function?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.