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 9246947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:36:45+00:00 2026-06-18T09:36:45+00:00

I wrote an ajax function named getContent() and structured like this getContent(whichcontent){//code here to

  • 0

I wrote an ajax function named getContent() and structured like this

getContent(whichcontent){//code here to get content}

The specific code is here:

function getXmlHttpRequestObject() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Not IE
  } else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
  } else {
    alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
  }
}

var receiveReq = getXmlHttpRequestObject();

var page_id = 1;

function getContent(which_page,append){
  if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {    

    receiveReq.open("GET", 'spt/page_'+which_page, true);//get the text file

    receiveReq.onreadystatechange = function(){
        handleGetContent(which_page,append);
    }
    receiveReq.send(null);
  }     
}

function handleGetContent(which_page,append){
    if (receiveReq.readyState == 4) {        
        if(append == 1){
            $('#container').append("<div class='page' id='page_"+which_page+"'><div class='title'>围城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>");

        }
        if(append == 0){
          $('#container').prepend("<div class='page' id='page_"+which_page+"'><div class='title'>围城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>");          
        }
    }
}

And I used the getContent like this

$(document).ready(function (){
    getContent(1,1);
    getContent(2,1);
}

The problem is I only get one … and the other one with id page_2 doesn’t appear. I’m wondering whether the ajax function can be only called once in a js function or I just made the ajax function wrong. Somebody help me!! Thanks in advance.

  • 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-18T09:36:46+00:00Added an answer on June 18, 2026 at 9:36 am

    I think the code is ok but if you have doubt with ajax then you can use jquery for it, if not then you can a single function for ajax for example: http://www.w3schools.com/ajax/ajax_aspphp.asp

    Or try this code

    // change the function like
    function getContent(which_page,append){
        var receiveReq = getXmlHttpRequestObject();// Create object here
    
        receiveReq.open("GET", 'spt/page_'+which_page+'.txt', true);//get the text file
        //write the extension of the file
        receiveReq.onreadystatechange = function(){
            if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {    
                handleGetContent(receiveReq,which_page,append);
            }
    
        }    
        receiveReq.send(null); 
    }
    // add an extra parameter in this function
    function handleGetContent(receiveReq,which_page,append){
        if (receiveReq.readyState == 4) {        
            if(append == 1){
                $('#container').append("<div class='page' id='page_"+which_page+"'><div class='title'>围城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>");
    
            }
            if(append == 0){
              $('#container').prepend("<div class='page' id='page_"+which_page+"'><div class='title'>围城</div><p>" + receiveReq.responseText + "</p><div class='pagenum'>"+which_page+"</div></div>");          
            }
        }
    }
    $(document).ready(function (){
        getContent(1,1);
        getContent(2,1);
    });// change this line also
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why can't I pass a named function to the jquery ajax complete option. Here's
This is my Javascript code: expr = A simple string $.ajax({ type: GET, url:
Hallo, i am new to jquery, AJAX requests. Here is my html code in
I'm just trying to learn some ajax so I wrote some code for basically
So i have this output generated from a jQuery function i built (ajax) which
I wrote a jquery code to send values to a php file $('#def_formSubmit').live("click",function(){ var
I'm using an old random() function for creating a validation code for an AJAX
I wrote some ajax code that sends values to a php file for validation
i was wondering how/where to write a callback function after an ajax request is
With the upcoming rise of AJAX applications on the web, I wrote some own

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.