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

  • Home
  • SEARCH
  • 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 8940721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:06:46+00:00 2026-06-15T11:06:46+00:00

I am trying to make a function to handle my AJAX post stuffs. I

  • 0

I am trying to make a function to handle my AJAX post stuffs.
I was able to get it to send the data, but am having issue receiving data back from the request.

Essentially, I have a function called ajaxPost(paramArray, target), what this does is it accepts the parameters and puts them into a string to submit to the target.

I think the issue comes from ajaxReq.onreadystatechange, I think it is skipping over that and ending the script.

I do not know if I am properly explaining what I mean, here is the script:

function ajaxPost(paramArray, target){
    var param = '';
    var key;
    for (key in paramArray){
        param = param + key + '=' + paramArray[key] + '&';
    }
    param = param.substring(0, param.length - 1);

    var ajaxReq = new XMLHttpRequest();
    ajaxReq.onreadystatechange = function(){
        if(ajaxReq.readyState == 4 && ajaxReq.status == 200){
            return ajaxReq.responseText;
            document.write('Test?');
        }
    }
    ajaxReq.open('POST', target, true);
    ajaxReq.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
    ajaxReq.send(param);
}

This is the function that ‘does the work’, and here is an example of a function that calls the above function:

function submitPost(form){
    var title = form.title.value;
    var text = form.textBody.value;
    var name = form.nameOfPoster.value;
    var paramArray = new Array();
    paramArray['title'] = title;
    paramArray['text'] = text;
    paramArray['name'] = name;
    if (ajaxPost(paramArray, 'post.php')){
        location.reload(true);
    }
}

This essentially sends the parameters to ajaxPost(), and if it returns something it reloads the page.

As I said, I think it is ignoring that one part referenced at the beginning of this post. I am just wondering if I can halt the function until that data is received.

Please do not tell me to use jQuery, I am experimenting with javascript 🙂

Thanks.
Please do include other tips if you have them.

  • 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-15T11:06:47+00:00Added an answer on June 15, 2026 at 11:06 am

    AJAX is asynchronouse. The way you are trying to use it will not work as ajaxReq.onreadystatechange function will be executed after request is done. At the same time, script execution will not stop. So, this:

    if (ajaxPost(paramArray, 'post.php')){
            location.reload(true);
    }
    

    will be executed before ajaxReq.onreadystatechange

    What you should do, is to pass a callback to your function:

    function ajaxPost(paramArray, target, callback){
        var param = '';
        var key;
        for (key in paramArray){
            param = param + key + '=' + paramArray[key] + '&';
        }
        param = param.substring(0, param.length - 1);
    
        var ajaxReq = new XMLHttpRequest();
        ajaxReq.onreadystatechange = function(){
            if(ajaxReq.readyState == 4 && ajaxReq.status == 200){
                callback(ajaxReq.responseText);
                document.write('Test?');
            }
        }
        ajaxReq.open('POST', target, true);
        ajaxReq.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
        ajaxReq.send(param);
    }
    

    and than:

    function submitPost(form){
        var title = form.title.value;
        var text = form.textBody.value;
        var name = form.nameOfPoster.value;
        var paramArray = new Array();
        paramArray['title'] = title;
        paramArray['text'] = text;
        paramArray['name'] = name;
        ajaxPost(paramArray, 'post.php', function(res) { if(res)  location.reload(true); });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to make a function that retrieves a value from the db, but
I'm trying to make a function that holds state but is called with foo().
I'm trying to set a function that makes an Ajax call, but I must
I`m trying to make my Go server send POST request with an array of
I am trying to make a function to handle avatar uploads. My problem is,
I am currently trying to make my Ajax to post some informations from a
Im trying to make a function, that takes an input, determines its value, and
Im trying to make a function that will return an element of type point:
I am trying to make a function that creates classes, and append these classes
I'm trying to make a function that does different things when called on different

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.