通过本地主机(不是CLI)使用Azure函数进行操作 [英] Cors with Azure function from localhost (not CLI)
问题描述
我们在vue.js应用程序中使用axios来访问Azure函数.现在,我们收到此错误:
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'
}
}
有人遇到此错误吗?
推荐答案
要在不使用CLI且使用Visual Studio/VS Code时将CORS设置为在本地工作-您需要将local.settings.json文件添加到您的项目,如果它不在那里.
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"中添加CORS": "*"
,如下所示:
Then in your "local.settings.json" you can add CORS": "*"
like so:
{
"IsEncrypted": false,
"Values": {
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
}
}
更多信息: https://docs.microsoft.com/zh-CN/azure/azure-functions/functions-run-local
这篇关于通过本地主机(不是CLI)使用Azure函数进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!