首页 > 解决方案 > 使用 Axios 发表评论时不存在“Access-Control-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 问题。非常感谢任何提示。

标签: wordpressvue.jsaxiosgridsomewp-graphql

解决方案


推荐阅读