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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:38:13+00:00 2026-06-01T19:38:13+00:00

In Ajax.use , inside the callback function this refers to the callback function. However

  • 0

In Ajax.use, inside the callback function this refers to the callback function.

However outside of a callback definition this would refer to a particular instance of Ajax.

That is how I use this in Ajax.invoke -> this.use() to call a member method from another member method.

I need to call a member method inside of the callback function as well.

I guessed and put in

this.invoke()

but I dont’ think this is referring to the correct object as it is in the callback function. I think it refers to the callback function object.

/**
 *    Ajax
 */

var Ajax = ( function () 
{
    var Ajax = function (element) 
    {
        this.object = this.create();
    };
    Ajax.prototype.create = function() 
    {
        var request;
        try
        {
            request = new window.XMLHttpRequest();
        }
        catch( error )
        {
            try 
            {
                request = new window.ActiveXObject( "Msxml2.XMLHTTP" );
            }
            catch( error )
            {
                try
                {
                    request = new window.ActiveXObject( "Microsoft.XMLHTTP" );
                }
                catch( error )
                {
                    request = false;
                }
            }
        }
        // alert("Object Created Successfully");
        return request;
    };

    Ajax.prototype.use = function( param, ajax_func )
    {
        alert("Ajax.prototype.use Called");
        this.object.open( "POST", Global.GATEWAY, true );
        this.object.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
        this.object.setRequestHeader( "Content-length", param.length );
        this.object.setRequestHeader( "Connection", "close" );
        this.object.onreadystatechange = function()
        {
            if( this.readyState === 4 )
            {
                if( this.status === 200 )
                {
                    ajax_func( this.responseText );
                    return true;
                }
                else
                {
                    this.invoke( param, ajax_func );
                    return false;
                }
            }
        };
        this.object.send( param );
        return true;
    };

    Ajax.prototype.invoke = function( param, ajax_func )
    {
        var state = false,
            count = 1;
        while( state === false && count <= 5 )
        {
            if( count !== 1 )
            {
                alert( 'Ajax Object Use Failed | Try Again ');
            }
            state = this.use( param, ajax_func );
            count++;
        }
        return state;
    };

    return Ajax;

} () );
  • 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-01T19:38:15+00:00Added an answer on June 1, 2026 at 7:38 pm

    You are exactly right that inside your callback this is no longer a reference to your Ajax object. You need to create a reference to it that you can use inside the callback:

    Ajax.prototype.use = function( param, ajax_func )
    {
        alert("Ajax.prototype.use Called");
        var self = this;
        this.object.open( "POST", Global.GATEWAY, true );
        this.object.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
        this.object.setRequestHeader( "Content-length", param.length );
        this.object.setRequestHeader( "Connection", "close" );
        this.object.onreadystatechange = function()
        {
            if( this.readyState === 4 )
            {
                if( this.status === 200 )
                {
                    ajax_func( this.responseText );
                    return true;
                }
                else
                {
                    self.invoke( param, ajax_func );
                    return false;
                }
            }
        };
        this.object.send( param );
        return true;
    };
    

    Notice that I am defining var self = this; and using self rather than this inside of the callback.

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

Sidebar

Related Questions

I have been trying to use this inside an ajax function to refer the
Im trying to use AJAX inside a live()-function but it does not work. Any
In my ajax application I'm trying to use a recursive function inside of which
I'm trying to use a google visualisation, the column chart, inside an asp.net AJAX
I use ajax to run MySQL query and echo json_encode result, so that I
I have many forms that use AJAX (w/ jQuery) for validation and data submission.
While debugging jQuery apps that use AJAX, I often have the need to see
I want to start an application which will use ajax push, however the web
I have come across a website that appears to use Ajax but does not
I have a php page that also has another php page inside it, this

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.