0
  • [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 问题。非常感谢任何提示。

4

0 回答 0