2
\$\begingroup\$

Here are 3 methods which handle my ajax calls.

I loop until pass b.c. I've had problems with the Ajax Object working as expected. This is noted here

/**
 *    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;
                }
            }
        }
        return request;
    };
    Ajax.prototype.use = function( param, ajax_func )
    {
        var GATEWAY = 'class.ControlEntry.php';  
        var self = this;
        this.object.open( "POST", 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;
    };
    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;
} () );
\$\endgroup\$
8
  • \$\begingroup\$ What's your question? \$\endgroup\$
    – seand
    Commented Jan 13, 2012 at 16:53
  • \$\begingroup\$ First question would be how do I not loop my ajax...has anyone else had an issue with the 12030 error code...showing up about 10% of the time... \$\endgroup\$
    – user7459
    Commented Jan 13, 2012 at 17:15
  • \$\begingroup\$ It seems to me that if the AJAX request has a genuine failure (maybe the path doesn't exist, or there's a network issue, or some kind of error on the server); then you'll be stuck in an endless loop. Maybe you should limit the number of times that you call ajaxRepeat. Also, I don't much like the names of your functions; it's not very clear what each one does. \$\endgroup\$ Commented Jan 14, 2012 at 9:45
  • \$\begingroup\$ This code doesn't work, it does not belong here (faq). This is troubleshooting that belongs on SO. \$\endgroup\$
    – Paul
    Commented Jan 14, 2012 at 14:16
  • \$\begingroup\$ 'issue with the 12030 error code...showing up about 10% of the time' - This is softawre, working 90% of the time doesn't cut it. \$\endgroup\$
    – Paul
    Commented Jan 15, 2012 at 3:21

1 Answer 1

1
\$\begingroup\$

This is the real bad practice.

  1. The Ajax is Asynchronous, you never know how the response will back.
  2. Javascript is the single thread language, so that means in one time period just can run one function. in your repeatUseAjax function, you try to loop send ajax. the result will be you will send 5 times ajax call every time when the ajax response failure.And you will never know when the response suceess. what about first failure other success? what about always failure?loop forever?
\$\endgroup\$
1
  • \$\begingroup\$ I do not try and loop "send ajax". If ajax responds with a fail ( it has completed )..then I make another request. \$\endgroup\$
    – user7459
    Commented Apr 18, 2012 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.