Event that occurs before any AJAX request is made to the server to allow the user to examine or modify the parameters passed.
Object.defineProperty(DocumentFactory, 'prepareAjax',
get: function(),
set: function(value)
)
function prepareAjax.add(function(sender, e));
function prepareAjax.remove(function(sender, e));
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 |
SourceClass |
Name of the class making the request. For instance, this will be |
SourceMethod |
Name of the method making the request. For instance, this will be |
Cancel | Can be set to true by the event handler to cause the request to fail immediately. |
Settings |
An object of type |
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
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.
Parameter | Type | Description |
---|---|---|
sender | var | The source of the event. |
e | PrepareAjaxEventArgs | The event data. |