When attempting to make an AJAX call are you getting the following error?
XMLHttpRequest cannot load (then some path to the remote site). No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://yourAjaxCallingSite.com’ is therefore not allowed access.
This means the Server hosting the resource is not set up to be CORS compliant.
If you are able to administer the server and if that server happens to be an IIS Windows Server 2012 R2, then this post is for you.
Cross-origin resource sharing (CORS) solves the issue that prevents sharing web services or resources between sites on different servers. Here is a link if you want to read more Cross-origin_resource_sharing. CORS sets up a mean by which a browser and server can safely determine whether or not to allow cross-origin requests. This permits more functionality and greater freedom than requests restricted to same-origin. At the same time, it is secure – not simply allowing every cross-origin request. In fact this is a recommended standard of the W3C.
How To Add a CORS compliant HTTP Response Header to IIS Windows Server 2012 R2
In our case we will add the ‘Access-Control-Allow-Origin’ HTTP Response header.
- On the Windows server select the Internet Information Services (IIS) Manager application from the icons in the bottom bar or click the Windows icon and select “Server Manager”
- Navigate to the website you need to edit the response headers for.
- From the list or Icons related to the site you are editing, select “HTTP Response Headers”.
- After it opens look for “HTTP Response Headers”. It will say say, “Use this feature to configure HTTP headers that are added to the responses from the Web server.”
- Click “Add”
A dialog box will open. For name enter “Access-Control-Allow-Origin” and for Value enter an asterisk. Or, if want to restrict the interactions to queries from a particular site, then enter that domain.
- Then click the OK button.
That’s it! You’re done.