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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:48:15+00:00 2026-05-14T23:48:15+00:00

I’m writing a small ajax class for personal use. In the class, I have

  • 0

I’m writing a small ajax class for personal use. In the class, I have a “post” method for sending post requests. The post method has a callback parameter. In the onreadystatechange propperty, I need to call the callback method.

Something like this:

this.requestObject.onreadystatechange = function() {
    callback(this.responseText); 
}

However, I can’t access the callback variable from within the anonomous function. How can I bring the callback variable into the scope of the onreadystatechange anonomous function?

edit:

Here’s the full code so far:

function request()
{
    this.initialize = function(errorHandeler)
    {
        try {
            try {
                this.requestObject = new XDomainRequest();
            } catch(e) {
                try {
                    this.requestObject = new XMLHttpRequest();
                } catch (e) {
                    try {
                        this.requestObject = new ActiveXObject("Msxml2.XMLHTTP"); //newer versions of IE5+
                    } catch (e) {
                        this.requestObject = new ActiveXObject("Microsoft.XMLHTTP"); //older versions of IE5+
                    }
                }
            } 
        } catch(e) {
            errorHandeler();
        }
    }

    this.post = function(url,data,callback)
    {
        var response;var escapedData = "";
        if (typeof data == 'object') {
            for (i in data) {
                escapedData += escape(i)+'='+escape(data[i])+'&';
            }
            escapedData = escapedData.substr(0,escapedData.length-1);
        } else {
            escapedData = escape(data);
        }
        this.requestObject.open('post',url,true);
        this.requestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        this.requestObject.setRequestHeader("Content-length", data.length);
        this.requestObject.setRequestHeader("Connection", "close");
        this.requestObject.onreadystatechange = function()
        {
            if (this.readyState == 4) {
                // call callback function
            }
        }
        this.requestObject.send(data);
    }
}
  • 1 1 Answer
  • 1 View
  • 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-14T23:48:16+00:00Added an answer on May 14, 2026 at 11:48 pm

    Just pass the callback function together with the rest of the arguments

    this.post = function(url, data, callback) {
        ...
        this.requestObject.onreadystatechange = function() {
            if (this.readyState == 4) {
                callback(this.responseText);
            }
        };
        ...
    }
    

    And then

    foo.post("foo.html", {foo:"bar"}, function(result){
        alert(result);
    });
    

    By the way, this is a better way to convert the data into a proper string

    var q = [];
    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            q.push(key + "=" + encodeURIComponent(data[key]));
        }
    }
    data = q.join("&"); //data can now be passed to .send()
    

    encodeURIComponent is the proper function to use here as encode will not escape data properly

    If you want to get a ready made function for all of this you can take a look here http://github.com/oyvindkinsey/easyXDM/blob/master/src/easyXDM.js#L358

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.