Search code examples
asp.net-web-apiazure-active-directoryoffice-jsoffice-addinsoffice-dialog-api

Sending an AJAX get request to a API secured using Azure Active Directory


I have created an office-js addin which is opening a dialog-api which is hosted as an Azure app service. The dialog-api send an AJAX call to an asp.net web api (which is secured using Azure AD).

While calling the web api I am getting thie error :

enter image description here

The code for the Ajax call :

 $.ajax({
                    url:'.../api/DocFile4/GetVersions',
                    type: 'GET',
                    crossDomain: true,
                    async: false,
                    contentType: 'application/json;charset=utf-8',
                }).done(function (data) {
                    console.log("success");
                }).fail(function (status) {
                    console.log('Error', 'Could not communicate with the server.');
                });

The Office-js addin and dialog-api are hosted on the same app sevice while the asp.net web api is hosted on a separate app service, both are the app services are in the same resource group.


Solution

  • The issue is due to CORS policy in your browser which is to secure your request.It will not allow parameters from external websites. You need set your website to allow requests as mentioned in the article.