前提・実現したいこと
ベーシック認証のかかったwordpressのカスタム投稿のapiを外部サイトのajaxで受け取りたい。
発生している問題・エラーメッセージ
Access to XMLHttpRequest at 'https://xxx' from origin 'https://xxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
試したこと
[ functions.php ] add_action( 'rest_api_init', function() { remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); add_filter( 'rest_pre_serve_request', function( $value ) { header( 'Access-Control-Allow-Origin: ' . $origin ); header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' ); header( 'Access-Control-Allow-Credentials: true' ); return $value; }); }, 15 ); --- [ js ] $.ajax({ url: url, username: "username", password: "password", type: 'POST', dataType: 'json', }) .done(function (data) { }).fail(function () {});
[ functions.phpに追加 ] function my_customize_rest_cors() { remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); add_filter( 'rest_pre_serve_request', function( $value ) { header( 'Access-Control-Allow-Origin: https://xxx' ); header( 'Access-Control-Allow-Methods: GET' ); header( 'Access-Control-Allow-Credentials: true' ); return $value; } ); } add_action( 'rest_api_init', 'my_customize_rest_cors', 15 ); [ 結果 ] Access to XMLHttpRequest at 'https://xxx/wp-json/wp/v2/xxx' from origin 'https:xxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
[ admin-ajax.phpに追加 ] header( 'Access-Control-Allow-Origin: https://xxx' ); header( 'Access-Control-Allow-Methods: GET' ); header( 'Access-Control-Allow-Credentials: true' ); [ 結果 ] Access to XMLHttpRequest at 'https://xxx/wp-json/wp/v2/xxx' from origin 'https:xxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
[ .htaccessに追加 ] Header set Access-Control-Allow-Origin: http://xxx Header set Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE Header set Access-Control-Allow-Credentials: true [ 結果 ] Access to XMLHttpRequest at 'https://xxx/wp-json/wp/v2/xxx' from origin 'https:xxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Request URL: https://xxx/wp-json/wp/v2/xxx Request Method: OPTIONS Status Code: 401 Remote Address: xx.xx.xx.xx Referrer Policy: no-referrer-when-downgrade content-length: 2784 content-type: text/html date: Sun, 23 Aug 2020 05:22:28 GMT etag: "ae0-57776badffe36" last-modified: Fri, 05 Oct 2018 08:05:08 GMT server: nginx status: 401 www-authenticate: Basic realm="Member Site" :authority: xx.xx.xx :method: OPTIONS :path: /wp-json/wp/v2/xxx :scheme: https accept: */* accept-encoding: gzip, deflate, br accept-language: ja,en-US;q=0.9,en;q=0.8 access-control-request-headers: authorization access-control-request-method: POST cache-control: no-cache origin: https://xxx pragma: no-cache referer: https://xxx sec-fetch-dest: empty sec-fetch-mode: cors sec-fetch-site: cross-site user-agent: xxx
function.phpやhttp.php、admin-ajax.php、.htaccessなどにもheader( 'Access-Control-Allow-Origin: ')を追加してみましたがダメでした。
ベーシック認証がかかっているとワイルドカードは使えないという情報も見たのでheader( 'Access-Control-Allow-Origin: https://***')とアドレスを指定してみましたがうまくいきません。
こちらの記事も試して見ましたができませんでした。
カスタム投稿のapi自体はちゃんと吐き出されています。
corsを回避できる方法はありますでしょうか?
何卒宜しくお願い申し上げます。
あなたの回答
tips
プレビュー