prepareAjax Event

Summary

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

Syntax
TypeScript
JavaScript
Object.defineProperty(DocumentFactory, 'prepareAjax', 
   get: function() 
) 
static readonly prepareAjax: PrepareAjaxEventType; 
Remarks

A user can add a handler to this event, which will fire before every AJAX request the Document library makes. An application can trigger this event programmatically with cancelFromPrepareAjax.

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:

NOTE: When using Microsoft Edge, Get requests such as GetSvg, GetSvgBackImage and GetImage don't have cookies. This will cause requests that expect an authentication cookie to fail. For an example resolution to this issue, see the LEADTOOLS JavaScript Document Viewer Demo. In the demo, select Preferences > Use AJAX image loading, then load a new document (the setting is on a per-document basis). This enables sending cookies with requests. This changes the image loading from being an "image.src = [url]" to an XMLHttpRequest, which matches the other API calls and gives greater functionality.

Member Value
The event source parameter

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

SourceClass

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

SourceMethod

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

Cancel

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

Settings

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 is the method we are interested in 
if (args.sourceClass != "DocumentFactory" || args.sourceMethod != "loadFromUri") { 
// No 
return; 
} 
 
// 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 
lt.Documents.DocumentFactory.prepareAjax.add(myPrepareAjaxHandler); 
 
// 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
ParameterTypeDescription
sendervarThe source of the event.
e

PrepareAjaxEventArgs

The event data.

Refer to Promises in the Document Service Library and Document Application for more information.

Requirements

Target Platforms

Help Version 22.0.2023.2.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.