Skip to content
Franco Mussopower pages
Code snippet

Portal Web API Wrapper AJAX

Copy it with the button on the code. For the why and the walkthrough, see “Authenticate with Power Pages’ Portal Web API using the Wrapper AJAX function”.

Portal Web API Wrapper AJAXHTML
<script>
(function(webapi, $){
    function safeAjax(ajaxOptions) {
        var deferredAjax = $.Deferred();
        shell.getTokenDeferred().done(function (token) {
            // add headers for AJAX
            if (!ajaxOptions.headers) {
                $.extend(ajaxOptions, {
                    headers: {
                        "__RequestVerificationToken": token
                    }
                });
            } else {
                ajaxOptions.headers["__RequestVerificationToken"] = token;
            }
            $.ajax(ajaxOptions)
                .done(function(data, textStatus, jqXHR) {
                    validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
                }).fail(deferredAjax.reject); //AJAX
        }).fail(function () {
            deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
        });
        return deferredAjax.promise();
    }
    webapi.safeAjax = safeAjax;
})(window.webapi = window.webapi || {}, jQuery)
</script>

Search

Book a discovery call

30 minutes. Bring your trickiest Power Pages problem — I’ll tell you honestly whether I’m the right fit.