wordpress - 使用 Axios 发表评论时不存在“Access-Control-Allow-Origin”标头
问题描述
- [CORS 政策错误]
- [ Gridsome 作为前端和 Axios 用于 HTTP 请求]
- [WP-Graphql v1.6.7]
- [已安装 WPGraphQL Cors]
- [从源“http://my_gridsome_front.com”访问“https://my_wp_backend.com”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问控制” -Allow-Origin' 标头出现在请求的资源上。]
axios 请求
addComment() {
axios({
url: 'https://my_wp_backend.com',
method: 'post',
headers: {
'Access-Control-Allow-Origin': '*',
},
withCredentials: true,
data: {
query: `mutation CREATE_COMMENT {
createComment(input: {
commentOn: 329,
content: "Lorem ipsum",
author: "John"
}) {
success
comment {
id
content
author {
node {
name
}
}
}
}
}`
}
})
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
}
我也为我的主题添加了一个过滤器
add_filter(
'graphql_response_headers_to_send',
function( $headers ) {
$headers['Access-Control-Allow-Origin'] = 'https://my_gridsome_front.com';
return $headers;
}
);
在 WPGraphQL Cors 设置中,将站点地址添加到“Access-Control-Allow-Origin”标头 = 已选中
安装 CORS 插件后,我仍然无法克服 CORS 错误,遵循文档中的所有说明并处理所有相关的 repo 问题。非常感谢任何提示。
解决方案
推荐阅读
- android - 链接到上一个 Flutter 的后退按钮
- charts - Chart.JS DataLabels 与图表图例重叠
- html - CSS 纵横比 100vw 或 100vh 取决于窗口大小
- puppeteer - 如何使用 Puppeteer 以编程方式滚动范围输入栏中的值?
- sql - 将日历表与数据表连接并填写空白数据
- ionic-framework - Ionic React - Redux - IonInput OnIonChange 未将值传递给操作
- azure-ad-b2c - 自定义电子邮件验证示例似乎无法正常工作
- python-3.x - 如何编写 Mock unittest 来检查 git_status?
- python - 公会成员
- c# - 将 Razor 库资源移动到 MVC 发布文件夹