The Official Blog of Patcoola

Im just a dude watching over my parents,
and trying to make cartoons and video games from home.

AJAX Class [JavaScript]

, By Patcoola

Title: AJAX Class
Programming Language: JavaScript
Date Created: 2023-02-28

Purpose: Communicate and exchange data with AJAX.

Description: An asynchronous JavaScript class based on instances. This class communicates with AJAX in an easy to manage application programming interface (API). Multi-instances allows for many called from a single API.

No legacy support.

This class does not support sending JSON, XML, BLOB data types. We recommend using an encoder/converter instead.

Update 2023-03-02: Fixed send GET and POST data.
Update 2023-03-02: Added 2047 total URL characters check to ADDRESS and REQUEST functions for GET method.

Advertisement

Donate Now!

This might not be the best time, but making a blog is not easy. Would you consider donating?


Methods

AJAX_class()

The class

class.getLastInstanceName()

A function to return the name of the last instance created

class.instance.has( name )

Function to check if an instance exists

class.instance.create( [name] )

Function to create an instance, if no name is provided then a number is assigned

Returns false if there is an error or returns the instance name

class.instance.list()

Function to return an array for instance names

class.instance.active()

Function to return an array of instances that are active

class.instance.count()

Function to return the number of instances in existence

class.instance.remove( name )

Function to remove an instance, works only if an instance is not active

Returns true or false

class.instanceByName( name )

A pointer for sub functions

class.instanceByName( name ).method( [GET|POST] )

Function to get or set the communication method. Takes or returns GET or POST

Default is GET

class.instanceByName( name ).address( [url] )

Function to get or set the web address

If method is GET, checks if the total URL is under 2047 characters

class.instanceByName( name ).request( name, [value] )

Function to get or set the an item to be sent

If method is GET, checks if the total URL is under 2047 characters

class.instanceByName( name ).timeout( [value] )

Function to get or set the time in seconds till the send request is cancelled, default 16 seconds; set to zero for no limit

class.instanceByName( name ).onResponse( function )

Function to set and execute the receive data event when the response has been received

One object argument is passed to the function, the response object

class.instanceByName( name ).onError( function )

Function to set and execute the receive data event when an error occurs

One object argument is passed to the function, the response object

class.instanceByName( name ).isActive()

Function to return if the call is active, returns true or false

class.instanceByName( name ).send()

Function to send the AJAX call

class.instanceByName( name ).abort()

Function to abort the send request; The instance must be active

Response

The on events can pass a single argument containing the response object.

name

The name of the instance

requestTime

The time in milliseconds when the request was made

readyState

The ready state

response

The response (the data returned), see response type

responseType

The data type of the data returned in response

responseURL

The complete URL from the data received

responseHeaders

The complete response header from the data received

responseTime

The time in milliseconds when the request was completed

status

The status from the response

statusText

The status text from the response

duration

The total time in milliseconds to complete the request

event

Determines the event origin, returns: timeout, error, abort, load

Errors

The following is considered an error.

Status Codes: 400 to 500+

Time Out

The following is considered a time out.

Status Codes: 408 and 504

Send

GET sends data by the URI and has a total URL character limit of 2047.

(Note, although the lowest limit is 2048, the limit was set to 2047 because of http and https redirects/correction)

Names with spaces, the spaces are automatically replaced with _ underscores as most servers do.

Values are automatically URI encoded with the UTF-8 character set.

onError and onResponse Events

onError will execute before onResponse. Note that onResponse can be used with object.event to process any situation and makes onError optional.

Example Usage

const ajax = new AJAX_class();

ajax.instance.create(‘my AJAX’);
ajax.instanceByName(‘my AJAX’).method(‘GET’);
ajax.instanceByName(‘my AJAX’).address(‘page.php’);

ajax.instanceByName(‘my AJAX’).request(‘name’, ‘Jack’);
ajax.instanceByName(‘my AJAX’).request(‘city’, ‘The Mouse Jaw’);

ajax.instanceByName(‘my AJAX’).onResponse(function (obj) {
console.log(obj[‘response’]);
});

ajax.instanceByName(‘my AJAX’).send();

 

Advertisement

Donate Now!

After a hard day, I like to relax with a cold jar of pickle juice and a hardy fart. If you like my blog, please consider donating.


 

Post Meta

Share Post

 

Comments: Write Comment

Be the first to write a comment.


Leave a Comment


All comments are reviewed before publishing. Comments must be related to the page topic, must not be spam, and must comply with the criminal code of Canada.