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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:13:38+00:00 2026-05-23T00:13:38+00:00

var ajax = { xmlHTTP: function() { xml = new XMLHttpRequest(); return xml; },

  • 0
var ajax = {
xmlHTTP: function() {
    xml = new XMLHttpRequest();
    return xml;
},
xmlHTTPfunction: function() {
    if (xml.readyState == 4 && xml.status == 200)  {
        document.getElementById("te").innerHTML = xml.responseText; 
         alert(xml.status);
         alert(xml.responseText);
     }

},
xmlHTTPopen: function(url) {
    xml.open("POST", url, true);
},
xmlHTTPheader: function() {
    ajax.xmlHTTP().setRequestHeader("Content-type","application/x-www-form-urlencoded");
},
xmlHTTPsend: function(string) {
    xml.send(string);
},
ajaxCall: function() {
    ajax.xmlHTTP();
    ajax.xmlHTTPopen('testx.php');
    ajax.xmlHTTPsend('test');
    return 1;
}
}
   if(ajax.ajaxCall() == 1) {
document.getElementById("te").innerHTML = xml.responseText; 

}
  • 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-23T00:13:38+00:00Added an answer on May 23, 2026 at 12:13 am

    Most of the functions refer to the xml variable which is poorly scoped. That is to say, when you call ajax.xmlHTTPopen there’s nothing to say that the xml variable has any value inside said function.

    The simplest way to provide better scoping would be to define the xml variable as a property of the ajax object; here is an example with minimal changes to your code:

    var ajax = {
        xmlHTTP: function() {
            this.xml = new XMLHttpRequest();
            this.xml.onreadystatechange = this.xmlHTTPfunction();
        },
        xmlHTTPfunction: function() {
            if (this.xml.readyState == 4 && this.xml.status == 200)  {
                document.getElementById("te").innerHTML = this.xml.responseText; 
                 alert(this.xml.status);
                 alert(this.xml.responseText);
             }
        },
        xmlHTTPopen: function(url) {
            this.xml.open("POST", url, true);
        },
        xmlHTTPheader: function() {
            this.xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        },
        xmlHTTPsend: function(string) {
            this.xml.send(string);
        },
        ajaxCall: function() {
            ajax.xmlHTTP();
            ajax.xmlHTTPopen('testx.php');
            ajax.xmlHTTPsend('test');
            return 1;
        }
    }
    

    Note that, as you’ve written it, the ajax.ajaxCall is non-blocking, so it will always return 1 immediately, even before the response arrives (or is necessarily sent) — to that end, you have to assign a callback function to the XMLHtpRequest object’s onreadystatechange event, to handle the response when it eventually comes back.

    If you change the third parameter of xml.open to false the xml.send call will block until a response arrives, in which case you don’t need to assign a callback. The alternative implementation may look like this:

    var ajax = {
        xmlHTTP: function() {
            this.xml = new XMLHttpRequest();
        },
        xmlHTTPopen: function(url) {
            this.xml.open("POST", url, false);
        },
        xmlHTTPheader: function() {
            this.xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        },
        xmlHTTPsend: function(string) {
            this.xml.send(string);
        },
        ajaxCall: function() {
            ajax.xmlHTTP();
            ajax.xmlHTTPopen('testx.php');
            ajax.xmlHTTPsend('test');
            return 1;
        }
    }
    if(ajax.ajaxCall() == 1) {
        document.getElementById("te").innerHTML = ajax.xml.responseText; 
    }
    

    However in my experience, this is almost never used in the real world.

    Also: your code never calls the xmlHTTPheader function, although there’s no harm in that.

    I can’t think of a better proposal given your current code; have you looked at jQuery and its implementation of ajax?

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

Sidebar

Related Questions

I made this bookmarklet: javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})() Formatted code: // Add in jQuery var
Ok so i have this javascript function getpreconfigproducts(str1,str2) { var url=ajax_preconfig_products.php?; url=url+id=+str1+&cid=+str2; xmlHttp =
I'm doing a jQuery AJAX post like this: var form = $(#formid); form.submit(function() {
function checkSession(){ $.ajax({url: session.php, success: function(data){ if( data == 1){ var postFilen = 'msg.php';
I'm trying to pass this to a PHP script through AJAX: var answers={}; for
I have the following piece of code: // setup the AJAX request var pageRequest
var htm = $.ajax({ type: GET, url: ./viewforms, async: false }).responseText; var myObject =
$.getJSON('ajax_popup.php', function(data) { var popupDiv = decodeURIComponent(data.data); $('body').append( popupDiv ); }); This piece of
I have this code for doing an ajax request to a webservice: var MyCode
I am working on the jsp/servlet/ajax application. I use XMLHttpRequest to pass values from

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.