從使用 Nginx 的 Google Cloud 遷移站點后,我一直在努力解決此錯誤,現在我將托管更改為 Apache 服務器。
該網站分為 2 個部分,前端(托管在 Vercel NextJS 上)和后端是一個 WordPress 站點,用作 GraphQL 的 API(這是我遷移的那個)我使用了 GraphQL 插件,現在來自前端的請求-end 收到此錯誤:
CORS 策略已阻止從源 [前端域] 訪問 [后端域] 處的 XMLHttpRequest:請求的資源上不存在“Access-Control-Allow-Origin”標頭。
我已將此添加到 .htaccess 檔案中:
Header add Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE"
Header set Access-Control-Allow-Headers "Content-Type"
我在后端和前端都使用 Cloudflare。
不知道還能做什么。我試圖回到以前的托管,它作業得很好,所以我不知道是什么問題!請幫忙!謝謝!:)
uj5u.com熱心網友回復:
在主題中的functions.php檔案中添加此過濾器并確保啟用了SSL
add_filter('rest_pre_serve_request', 'cors_headersxxxx', 0, 4 );
function cors_headersxxxx() {
header( 'Access-Control-Allow-Origin: *');
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
header( 'Access-Control-Allow-Credentials: true' );
header( 'Access-Control-Allow-Headers: Authorization, Content-Type, X-Requested-With, X-Mystore-Cartkey, X-USER-ID' );
//header( 'Access-Control-Expose-Headers: X-Mystore-Cartkey' );
}
add_filter('rest_api_init', 'wp_mystore_jwt_add_cors_support');
function wp_mystore_jwt_add_cors_support() {
$enable_cors = true;
if ($enable_cors) {
$headers = 'Access-Control-Allow-Headers, X-Requested-With, Content-Type, Accept, Origin, Authorization';
header( sprintf( 'Access-Control-Allow-Headers: %s', $headers ) );
}
}
uj5u.com熱心網友回復:
是固定的!顯然,我必須通過保存永久鏈接設定來重繪 永久鏈接:設定 > 永久鏈接
我在這里找到了解決方案:https : //github.com/wp-graphql/wp-graphql/issues/213
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/373147.html
標籤:WordPress的 阿帕奇 图形 下一个.js
