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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:37:19+00:00 2026-05-27T00:37:19+00:00

I have a problem with an AJAX call that i make to login a

  • 0

I have a problem with an AJAX call that i make to login a user. The code looks like this:

function loginUser()
{
    var xmlhttp;
    if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }

    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //alert(xmlhttp.responseText);
            var responseString = xmlhttp.responseText;
            if(responseString.indexOf("loginStatus:approved") != -1) {
                document.getElementById("logInBar").innerHTML = "Welcome " + getCookie("userLoggedOnfn") + " <a href=\"logoutUser.php\">Logout</a>";
            }
        }
    }

    var params = "loginUsername=" + document.getElementById("loginUsername").value +
                 "&loginPassword=" + document.getElementById("loginPassword").value +
                 "&d=" + new Date().getTime();

    //alert(params);

    xmlhttp.open("POST", "loginUser.php", true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);

        //alert("."); //this alert box is needed to make it work if it's asynchronous
}

The problem is that if i have it like this, it never reaches readyState == 4, or 3 for that matter… it only gets to 2. And if I break the js operations before it reaches readyState == 3 in the debugger in chrome, it gets to readyState == 4. And if I do it synchronous, it works, or if I add the alert box in the end… it’s like it needs to pause before it can reach readyState == 4 or something… So what I’m i doing wrong?

Btw, i need to do this in pure AJAX because it’s a school assignment…

  • 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-27T00:37:20+00:00Added an answer on May 27, 2026 at 12:37 am

    I found the problem! It was in the html code.

    I had the login button and text boxes inside a form tag, and chrome automatically refreshed the whole page when i pressed the button inside the form tag. So the AJAX call was messed up because of the reloading of the page.

    I just removed the form tags and the problem was solved.

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

Sidebar

Related Questions

The problem is the following. We have lots of ajax call via $.ajax function.
I have this problem that my sites uses alot of ajax and when a
I'd like to be able to make an Ajax call using JSF/Seam/RichFaces and have
I would like to have a function that returns the repsonseText of a jQuery.ajax()
My trying to make an Ajax call to a PHP function that pulls out
I have users making ajax call while typing. The problem is that it makes
I have a dropdown that triggers an ajax call when its changed: $('.travel-to').change(function(){ $.ajax({
In my JS I have the following ajax call, that bind the resulting json
I have some JavaScript that makes an AJAX call and, if the call fails,
I have an ajax call to a php script that goes into a database

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.