prepareAjax Event (DocumentFactory)


Event that occurs before any AJAX request is made to the server to allow the user to examine or modify the parameters passed.


JavaScript Syntax
Object.defineProperty(DocumentFactory, 'prepareAjax',  
	get: function(), 
	set: function(value) 
function prepareAjax.add(function(sender, e)); 
function prepareAjax.remove(function(sender, e)); 
TypeScript Syntax
static prepareAjax: void;


A user can add a handler to this event, which will fire before every AJAX request the Documents library makes.

In the case of methods that retrieve elements, such as GetImageElement, this event will fire when an AJAX loading method is used. If an ImageLoader is provided as a parameter, AJAX settings will be retrieved from UrlMode and AjaxOptions. Otherwise, the default options from ElementAjaxMethod and ElementUrlMode are used. See Documents-ImageLoading for more information.

The event data is of type PrepareAjaxEventArgs and will contain the following:

Member Value
The event source parameter

The object making the request. For instance, this will be null for DocumentFactory.loadFromUri since the method is "static" and the Document or DocumentPage object for Document.convert and DocumentPage.getText.


Name of the class making the request. For instance, this will be "DocumentFactory" for DocumentFactory.loadFromUri, "Document" for Document.convert and "DocumentPage" for DocumentPage.getText.


Name of the method making the request. For instance, this will be "loadFromUri" for DocumentFactory.loadFromUri, "convert" for Document.convert and "getText" for DocumentPage.getText.

Cancel Can be set to true by the event handler to cause the request to fail immediately.

An object of type JQueryAjaxSettings Object, which in turn holds the headers, method, url, and other request properties. The data property of JQueryAjaxSettings will be a JSON-stringified object (a string) for POST requests, and an object for GET requests.

Any changes made to that object will be used in the request. This will allow a user to modify the headers of any request to set authorization headers as they wish for instance.

The following shows how to use PrepareAjax to intercept calls to LoadFromUri and adding custom authentication headers that will be parsed by the server.

Client side JavaScript code:

// Our interceptor function 
var myPrepareAjaxHandler = function (sender, args) { 
// Check if this the method we are interested in 
if (args.sourceClass != "DocumentFactory" || args.sourceMethod != "loadFromUri") { 
// No 
// Yes, add to the headers. If headers do not exist, initialize first 
if (!args.settings.headers) { 
args.settings.headers = {}; 
args.settings.headers["my_custom_auth_header_key"] = "my_custom_auth_header_val"; 
// Add our handler to the static PrepareAjax event 
// Now call loadFromUri and we should be able to intercept and add our custom headers 
lt.DocumentFactory.loadFromUri(uri, options); 

Server side .NET code:

// An Example Of Getting Headers, this could be in FactoryController.cs 
public LoadFromUriResponse LoadFromUri(LoadFromUriRequest request) 
var headers = Request.Headers; 
if (headers.Contains("my_custom_auth_header_key")) 
var values = headers.GetValues("my_custom_auth_header_key"); 
if (values.Contains("my_custom_auth_header_val")) 
// Success, do something ... 
// rest of code 

If the handler aborts the request by setting the value of Cancel to true, then the request will fail immediately and the method will invoke the fail promise. ParseError can be used to obtain extra information on the error and in this situation, the value of IsCancelError will be true.

Event Data
sendervarThe source of the event.
ePrepareAjaxEventArgsThe event data.

Target Platforms

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
Leadtools.Documents Assembly
Click or drag to resize