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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:39:16+00:00 2026-05-24T10:39:16+00:00

i am having trouble getting this XMLHttpRequest to work properly, this is my first

  • 0

i am having trouble getting this XMLHttpRequest to work properly, this is my first time using ajax so i’m not sure if i’m formatting everything correctly. i have looked all over the web and i keep finding basically the same info and examples but certain elements are in different orders so i’m not sure which is correct and i’ve tried them all and nothing seems to work. here is my code:

function ajaxRequest(){
    var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
    if (window.XMLHttpRequest)
        return new XMLHttpRequest()
    else if (window.ActiveXObject){ 
        for (var i=0; i<activexmodes.length; i++){
            try{
                return new ActiveXObject(activexmodes[i])
            }
            catch(e){
                //suppress error
            }
        }
    }
    else
        return false
}
function getData(fileName){
    var fileLoc =encodeURI("assets/"+fileName+".html")
    alert(fileLoc)
    var request = new ajaxRequest()
    request.open("GET",fileLoc,true)
    var response = request.responseText
    alert(request.status)
    alert(response)
    request.send(null)
    return response
}
function home() {
    var data = getData("home")
    var contentDiv = document.getElementByClassName(content)
    contentDiv.innerHTML = data;
}

home is triggered when the user clicks on a div in the page. i know that getData is being accessed because the alerts pop up, however i get a status code of 0, this happened on both my local machine and on a live server. i read that localhosts can throw a 0 status regardless of the actual status but its happening on a live server as well. if someone could help me fix this issue and/or clarify the correct order of events in the function i would greatly appreciate it.

EDIT:
new code:

function getData(fileName){
    fileLoc = encodeURI("assets/"+fileName+".html")
    alert(fileLoc);
    request.onreadystatechange = processData;
    request.open("GET",fileLoc, false);
    request.send();
    alert(request.readyState);
    alert(response);
}
function processData(){
    if (request.readyState==4){
        if (request.status==200){
            document.getElementsByClassName('content').innerHTML = request.responseText;
        }
        else{
            alert("An error has occured making the request");
        }
    }
}
  • 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-05-24T10:39:16+00:00Added an answer on May 24, 2026 at 10:39 am

    You are telling XMLHTTPRequest to send the request asynchronous, which means the rest of your script gets executed while you wait for the response. You will get a reponse code 0 (uninitialized) and an empty response, because at the time you return from the function that is the current status and response.

    Instead you want to define a function to call when the state changes, or let XMLHTTPRequest work synchronous.

    Please refer to this tutorial for a simple example that should help you out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.
Having trouble getting this to work. What's strange is that I have 10 bookmarks
I'm having trouble getting this <div> to automatically update using jQuery. It's essentially a
I am having trouble getting this to work so any help would be appreciated!
I am having trouble getting this to work. I have variables initiated in main
I am having trouble getting this to work correctly (obviously) - I am ALMOST
I'm having trouble getting this to work...it just replaces the entire page with the
I am having trouble getting a regex to work. This is the string. some
Im having trouble getting this to work, here´s a quick overview of the idea.

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.