Page 1 of 1

pui.AjaxRequest suppressAlert property

Posted: Thu Sep 17, 2015 12:10 pm
by jpmurtha
Dealertrack would like to be able to suppress the browser alert popup that displays when an invalid url is passed to pui.AjaxRequest.
[img]
DMSCORE-6285-3.jpg
DMSCORE-6285-3.jpg (12.74 KiB) Viewed 378 times
[/img]
Setting the suppressAlert property to true or false does not prevent this popup from showing. The javascript code is below

Code: Select all

Onload
var apiVersion = tripos.isAvailable("http://localhost:8080/api");
if (apiVersion !== null) {
    changeElementValue('version', apiVersion);
} else {
    changeElementValue('version', "triPOS not available");
}

/*
 * check for existance of triPOS restful API
 *
 * Will return a string containing the version of the triPOS api
 * or null which indicates some kind of error occurred and the
 * error details can be determined from variables
 * tripos_error_code and tripos_error_message.
 */
tripos.isAvailable = function(dtUrl) {
    var dtRequest, dtJsonResponse;
    tripos.resetError();

    // validate parameters
    if (dtUrl === null || dtUrl === undefined) {
        // null or missing url error
        tripos.setError("", "null or missing url parameter");
        return null;
    }

    dtRequest = new pui.AjaxRequest(dtUrl);

    if (dtRequest) {
        dtRequest.method = "GET";
        dtRequest.async = false;
        dtRequest.setRequestHeader('tp-application-id','3028');
        dtRequest.setRequestHeader('tp-application-version','1.0.0');
        dtRequest.setRequestHeader('tp-application-name','MyApplication');
        dtRequest.setRequestHeader('tp-authorization','Version=1.0, Credential=7c2dcd15-f327-462c-805e-0a4b7389a77a');
        dtRequest.setRequestHeader('accept', 'application/json');
        dtRequest.suppressAlert = false;

        dtRequest.send();

        if (dtRequest.getStatus() !== 200) {
            tripos.setError("", "http status " + dtRequest.getStatus());
            return null;
        } else {
            dtJsonResponse = JSON.parse(dtRequest.getResponseText());
            if (dtJsonResponse) {
                return dtJsonResponse._links[0].rel;
            } else {
                // json parsing error
                tripos.setError("", "error parsing api service response!");
                return null;
            }
        }
    } else {
        // error creating pui.AjaxRequest
        tripos.setError("", "error creating pui.AjaxRequest!");
        return null;
    }
};

Re: pui.AjaxRequest suppressAlert property

Posted: Thu Sep 17, 2015 12:43 pm
by jpmurtha
Here is the solution that I came up with. Had the idea to handle the exception in my code instead of letting it be handled by genie. Put code in a try catch as follows and it the browser popup no longer displays.
try {
dtRequest.send();
}
catch(error) {
return null;
}

Re: pui.AjaxRequest suppressAlert property

Posted: Thu Sep 17, 2015 12:53 pm
by Alex
Yes, this is a good solution.

The suppressAlert property prevents alerts of unsuccessful AJAX requests, but it does not suppress JavaScript errors; which, in this case are reported by the onload event rather than the pui.AjaxRequest() class.