Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS with Azure function from localhost (not CLI)

We are using axios in a vue.js app to access an Azure function. Right now we are getting this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource.  Origin 'http://localhost:8080' is therefore not allowed access. 

We are trying to set response headers in the function this way:

context.res = {   body: response.data,   headers: {        'Access-Control-Allow-Credentials': 'true',     'Access-Control-Allow-Origin': 'http://localhost:8080',     'Access-Control-Allow-Methods': 'GET',     'Access-Control-Request-Headers': 'X-Custom-Header'   } } 

Has anyone run across this error?

like image 451
steverb Avatar asked May 03 '17 18:05

steverb


People also ask

How do I enable CORS on Azure function app?

If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. In order to find the source of this error, go to the Azure Portal, and navigate to the Function App under consideration, and locate CORS in the left side panel.

How do I enable CORS in Azure Apim?

Configure the cors policy in API Management for the following scenarios: Enable the interactive test console in the developer portal. Refer to the developer portal documentation for details. When you enable CORS for the interactive console, by default API Management configures the cors policy at the global scope.

How do I enable CORS in Azure Blob?

To enable CORS, you need to set the appropriate service properties using version 2013-08-15 or later for the Blob, Queue, and Table services, or version 2015-02-21 or for the File service. You enable CORS by adding CORS rules to the service properties.


2 Answers

For v3+ the following works:

p.s. note that location of Hosts is on the same level as Values and not under it (as in the answer by azadeh-khojandi https://stackoverflow.com/a/48069299/2705777)

Configure CORS in the local settings file local.settings.json:

{   "Values": {   },   "Host": {     "CORS": "*"   } } 
like image 24
Neil Avatar answered Sep 23 '22 03:09

Neil


To set CORS working locally when you are not using CLI and you are using Visual Studio/ VS Code - you need to add local.settings.json file into your project if it's not there.

Make sure "Copy to output directly" set to "copy if newer"

local.settings.json settings

Then in your "local.settings.json" you can add CORS": "*" like so:

{   "IsEncrypted": false,   "Values": {    },   "Host": {     "LocalHttpPort": 7071,     "CORS": "*"   } } 

More info: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local

like image 180
Azadeh Khojandi Avatar answered Sep 24 '22 03:09

Azadeh Khojandi