目标
我正在尝试从Vue 3类型化前端向Symfony 5 API发送请求。使用NelmioCorsBundle,该请求可以在Insommia(postman)中使用,但在Chrome、Safari和Firefox中无法使用。
错误信息
跨域资源共享(CORS)策略阻止访问' https://localhost:8000/api/users/6' 的请求,该请求来源为'http://localhost:8080'。响应预检请求未通过访问控制检查:所请求的资源上没有出现'Access-Control-Allow-Origin'头部字段。如果一个不透明的响应完全满足您的需求,请将请求的模式设置为“no-cors”以禁用CORS并获取资源。
前端(Vue)请求:
import { onMounted, ref } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Home',
setup () {
const message = ref('Please login...')
const store = useStore()
onMounted(async () => {
try {
const response = await fetch('https://localhost:8000/api/users/6', {
headers: { 'Content-Type': 'application/json' },
credentials: 'include'
})
console.log(response)
const user = await response.json()
message.value = `Hello ${user.name}`
await store.dispatch('setAuth', true)
} catch (e) {
console.log(e)
}
return {
message
}
})
}
}
Symfony 中的 NelmioCorsBundle 配置:
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
在 .env 文件中,CORS_ALLOW_ORIGIN 被定义为:
CORS_ALLOW_ORIGIN='^http?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
我的尝试
- 我尝试在NelmioCorsBundle和index.php中设置Access-Control-Allow-Origin头部。
- 我尝试将allow_origin设置为'http://localhost:8080'。
- 我尝试在index.php文件中编写代码来设置不同的头部,但最终只是遇到了错误。
- 我还尝试了文档中有关如何忽略New Relic上的preflight请求的建议。但这个
FilterResponseEvent
类不存在 (所以我重新安装了NelmioCorsBundle,但没有起作用.)
yourdomain
占位符是用于本地主机地址的吗? - Kerrial Beckett Newham$_SERVER['HTTP_REFERER']
的值是正确的,并且只发送一个Access-Control-Allow-Origin:
标头。您是否禁用了以前的 nelmio 安装? - yhu420--allow-http
标志运行,则一切都按预期功能。完整命令为symfony server:start --allow-http
。然而,这并没有回答如何正确设置它的问题。 :) - Kerrial Beckett Newham