This object provides some helper methods that allow you to make and filter Ajax requests.
Members
headers :string
Gets or sets the headers that will be defined with every AJAX request Viewer Control makes.
This is an ECMA 5 property that is defined only in browsers supporting ECMA 5. This property is not available in the older browsers like IE8. For the greatest browser compatibility, use the corresponding getter and setter methods.
Type:
- string
Example
// get
var headers = PCCViewer.Ajax.headers;
// set
PCCViewer.Ajax.headers = { 'My-Secret-Header': 'mysecretkey' };
overrideMethod :string
Gets or sets the overrideMethod that will be defined with every AJAX request Viewer Control makes.
This is an ECMA 5 property that is defined only in browsers supporting ECMA 5. This property is not available in the older browsers like IE8. For the greatest browser compatibility, use the corresponding getter and setter methods.
Type:
- string
Example
// get
var overrideMethod = PCCViewer.Ajax.overrideMethod;
// set
PCCViewer.Ajax.overrideMethod = function(options) {
options.headers['My-Secret-Header'] = 'mysecretkey';
};
Methods
getHeaders() → {Object}
Gets the headers object that will be sent with every request.
Note: Any header returned here will be sent with the request as long as the specific request doesn't use that same header with a different value. If you're expecting the header to exist on all requests, it should use a unique key that is not used by Accusoft.
Returns:
A copy of the headers object set with PCCViewer.Ajax#setHeaders.
- Type
- Object
Example
// The default headers are set for every request
PCCViewer.Ajax.setHeaders({
'My-Secret-Header': 'mysecretkey',
'Authorization': 'Token asdf1234'
});
// Retrieve the headers that have previously been set
PCCViewer.Ajax.getHeaders(); // returns { 'My-Secret-Header': 'mysecretkey', 'Authorization': 'Token asdf1234' }
getOverrideMethod() → {function}
Gets the override method that will be called with every request.
Note: The function that is returned with this object is passed by reference and is the same one set with setOverrideMethod. Use caution when interacting with it.
Returns:
The function defined with PCCViewer.Ajax#setOverrideMethod.
- Type
- function
Example
// A function is set to add a header to every request
PCCViewer.Ajax.setOverrideMethod(function(options) {
options.headers['My-Secret-Header'] = 'mysecretkey';
});
// Retrieve the override method that was previously set
PCCViewer.Ajax.getOverrideMethod(); // returns function
setHeaders(headers) → {Object}
Sets the headers object that will be sent with every request.
Notes:
- Overwrites any headers already set.
- Setting the
headers
as undefined will result in no additional headers being sent with every request. The headers that the request requires will still be present.
Parameters:
Name | Type | Description |
---|---|---|
headers |
Object |
An object containing the headers we will send with every request.
|
Throws:
-
If
headers
is not an object or undefined. -
- Type
- Error
Returns:
The PCCViewer.Ajax object.
- Type
- Object
Example
// getHeaders() returns an empty object if setHeaders() has not been called
PCCViewer.Ajax.getHeaders(); // returns {}
// After calling setHeaders() with an object, getHeaders() will return a copy of that object
PCCViewer.Ajax.setHeaders({ 'My-Secret-Header', 'mysecretkey' });
PCCViewer.Ajax.getHeaders(); // returns { 'My-Secret-Header', 'mysecretkey' }
// After calling setHeaders() without any arguments, getHeaders() will return an empty object
PCCViewer.Ajax.setHeaders();
PCCViewer.Ajax.getHeaders(); // returns {}
// The value can only be set to an object or undefined.
// All other data types throw.
PCCViewer.Ajax.setHeaders(null); // throws
PCCViewer.Ajax.setHeaders(1); // throws
PCCViewer.Ajax.setHeaders(true); // throws
PCCViewer.Ajax.setHeaders('b'); // throws
PCCViewer.Ajax.setHeaders([]); // throws
setOverrideMethod(method) → {Object}
Sets the override method that will be called with every request.
Notes:
- Overwrites any method already set.
- Setting the
method
as undefined will result in no override taking place. The request will proceed as usual
Parameters:
Name | Type | Description |
---|---|---|
method |
PCCViewer.Ajax~OverrideMethod |
A function that will be called with every request that allows you to override or modify the request.
|
Throws:
-
If
method
is not a function or undefined. -
- Type
- Error
Returns:
The PCCViewer.Ajax object.
- Type
- Object
Example
// getOverrideMethod() returns undefined if setOverrideMethod() has not been called
PCCViewer.Ajax.getOverrideMethod(); // returns undefined
// After calling setOverrideMethod() with a function, getOverrideMethod() will return the function by reference
PCCViewer.Ajax.setOverrideMethod(function(options) { options.url = 'http://accusoft.com/'; });
PCCViewer.Ajax.getOverrideMethod(); // returns function
// After calling setOverrideMethod() without any arguments, getOverrideMethod() will return undefined
PCCViewer.Ajax.setOverrideMethod();
PCCViewer.Ajax.getOverrideMethod(); // returns undefined
// The value can only be set to a function or undefined.
// All other data types throw.
PCCViewer.Ajax.setOverrideMethod(null); // throws
PCCViewer.Ajax.setOverrideMethod(1); // throws
PCCViewer.Ajax.setOverrideMethod(true); // throws
PCCViewer.Ajax.setOverrideMethod('b'); // throws
PCCViewer.Ajax.setOverrideMethod([]); // throws
Type Definitions
OverrideMethod(options) → {Promise|Undefined}
The function that you define with PCCViewer.Ajax#setOverrideMethod should follow this format
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
Properties
|
Returns:
- Optionally returns a then-able object (Promise) to prevent default execution from continuing.
- Type
- Promise | Undefined
Example
// This method can modify properties of the original request
PCCViewer.Ajax.setOverrideMethod(function(options) {
options.url = 'http://accusoft.com/';
});
// This method can prevent default execution from continuing
PCCViewer.Ajax.setOverrideMethod(function(options) {
var deferred = $.Deferred();
$.ajax({
url: options.url,
method: options.method,
headers: options.headers,
data: options.body,
mimeType: options.mimeType,
timeout: options.timeout,
}).then(
function(data, textStatus, jqXHR) {
deferred.resolve(new PCCViewer.AjaxResponse({
status: jqXHR.status,
statusText: textStatus,
headers: {
'Fake-Header': 'thisisnotreal'
},
responseText: jqXHR.responseText,
}));
},
function(jqXHR, textStatus, errorThrown) {
deferred.reject({
error: new PCCViewer.Error('Error', errorThrown),
response: new PCCViewer.AjaxResponse({
status: jqXHR.status,
statusText: textStatus,
headers: {
'Fake-Header': 'thisisnotreal'
},
responseText: jqXHR.responseText,
}),
});
}
);
return deferred.promise();
});