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?
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.
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.
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.
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": "*" } }
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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With