Using the Dynamics 365 web API within Dynamics 365 web resources is straightforward. We previously covered a web API in a few recipes; (Querying 365 data using the Web API endpoint and Building custom UI using Angular recipes of Chapter 2, Client-Side Extensions, and Connecting to Dynamics 365 from other systems OData (Java) recipes of Chapter 5, External Integration); however, accessing the web API from the client side on a different web application is a bit more challenging.
If you simply create a request using XMLHttpRequest
from a locally hosted web application to the web API, you will receive the following error:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401.
An unauthorized 401 exception is returned due to missing authentication details. In this recipe, we will build a single-page HTML application (SPA) with...