76

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?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
steverb
  • 1,415
  • 1
  • 10
  • 12
  • is CORS enabled on the server hosting your api code? – victor May 03 '17 at 18:16
  • Not sure what you mean. We are using Azure functions, so we don't configure the server. – steverb May 03 '17 at 18:20
  • so the resource that you are requesting from Azure is not configured with this header "Access-Control-Allow-Origin". So for example, if that header was set by the server like this "Access-Control-Allow-Origin": www.google.com it would mean that that azure function allows request to come from google. – victor May 03 '17 at 18:35
  • 1
    You should read about CORS requests. Basically, when the browser sees that the domains are different, it make a seperate call to the requested domain to get the "Access-Control-Allow-Origin" headers to see what external domains are allowed access to the server. If the domain you are requesting FROM is not listed in the headers, then the browser does not allow the request to proceed. – victor May 03 '17 at 18:38
  • 2
    Have you tried enabling CORS via the [Function App Settings](https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#cors)? – Pragna Gopa May 03 '17 at 18:44

9 Answers9

126

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://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local

Johan Karlsson
  • 494
  • 5
  • 7
Azadeh Khojandi
  • 3,806
  • 1
  • 30
  • 32
  • 13
    This is not working. The default VS Azure Function template already has this file and CORS set to *. But if you try to access the localhost API from another website running localhost (but different port) it will not work. – Morten_564834 Dec 03 '19 at 05:47
  • 2
    This worked for me. Adding the CORS to local.settings.json fixed the network error I was getting in the browser. Removing the CORS and testing again caused the error to return. – Todd Smith May 03 '22 at 06:40
  • This doesn't work with function runtime v4 – Paul Smith Aug 23 '23 at 21:58
47

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": "*"
  }
}
Neil
  • 7,482
  • 6
  • 50
  • 56
38

We got it working. It was a configuration in our Azure function. You go to "Platform Features" then "CORS". We added http://localhost:8080 to the list of "Allowed Origins" and then everything worked.

Elaboration For Production Environment Issues

I was having a problem on localhost, and on production (firebase hosted), trying to get my JavaScript Web app to interact with an Azure Function.

Cross-Origin Resource Sharing (CORS) allows JavaScript code running in a browser on an external host to interact with your backend.

In Azure Functions, click the features tab, and click the CORS block under "networking and security".

Add your domain as an allowed origin and hit save. This will fix the issue.

Matthew Rideout
  • 7,330
  • 2
  • 42
  • 61
steverb
  • 1,415
  • 1
  • 10
  • 12
16

Had same problem. On root of backend project, there's a file local.settings.json.

Added "CORS": "*" and "CORSCredentials": false in that file (following is the example), did mvn clean package -DskipTests=true on root, and mvn azure-functions:run -DenableDebug on the azure function directory.

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "<language worker>",
    "AzureWebJobsStorage": "<connection-string>",
    "AzureWebJobsDashboard": "<connection-string>",
    "MyBindingConnection": "<binding-connection-string>"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*",
    "CORSCredentials": false
  },
  "ConnectionStrings": {
    "SQLConnectionString": "<sqlclient-connection-string>"
  }
}

Reference:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos

QauseenMZ
  • 1,081
  • 8
  • 6
12

For those of you who are doing all of the above, but still not getting anything to work, it could be that your local.settings.json file is completely ignored. I don't know if this is because I'm using v3.

Go to Properties of your Project -> Debug -> Application arguments ->

host start --build --port 7071 --cors * --pause-on-error

Start your application

Morten_564834
  • 1,479
  • 3
  • 15
  • 26
2

You can enable the CORS from hosted environment in function app to add the web app URL refer the below screenshot.

Enable CORS in Function app

1

Note: TO allow all mark as "*"

vimuth
  • 5,064
  • 33
  • 79
  • 116
1

I had the same issue and the culprit was actually a typo in the Blazor-embedded-URI which Firefox displayed as a CORS error. Solution was just to realize that it had nothing to do with CORS and fix the mis-typed URI.

Alex Mann
  • 296
  • 3
  • 4
  • We also get very similar error in case the host is "not available". In my case the Azure Function Application was stopped and I got the following error message: `"origin 'https://REMOVED.core.windows.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 'No Access-Control-Allow-Origin' header is present on the requested resource."` – minus one Feb 02 '22 at 10:23
1

Please note that CORS policies should be activated on the server where the resource is hosted.

In my case, despite I was testing my API in local, I was accessing a resource on the real blob storage, where no CORS policy was set.

Activating the CORS policy on the blob storage solved the issue, in my case.

enter image description here

HAL9000
  • 3,562
  • 3
  • 25
  • 47
0

We had multiple CORS policies enabled and it didn't work. When we deleted every CORS policy and left only "*" (allow all) - it worked.

Andrei Kniazev
  • 103
  • 2
  • 8