前提・実現したいこと
【CORS】クロスドメインによるエラーを解決したい
WordPressを使って記事を配信しています
あるドメインでは配信出来るのですが別のドメインにするとクロスドメインエラーが出ます。
それを解決したい。
発生している問題・エラーメッセージ
Access to XMLHttpRequest at 'https://dev.〇〇.com/wp-admin/admin-ajax.php' from origin 'http://localhost:8100' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
core.js:6185 ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"https://dev.〇〇.com/wp-admin/admin-ajax.php","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://dev.〇〇.com/wp-admin/admin-ajax.php: 0 Unknown Error","error":{"isTrusted":true}} at resolvePromise (zone-evergreen.js:798) at zone-evergreen.js:705 at rejected (tslib.es6.js:72) at ZoneDelegate.invoke (zone-evergreen.js:364) at Object.onInvoke (core.js:41275) at ZoneDelegate.invoke (zone-evergreen.js:363) at Zone.run (zone-evergreen.js:123) at zone-evergreen.js:857 at ZoneDelegate.invokeTask (zone-evergreen.js:399) at Object.onInvokeTask (core.js:41253) zone-evergreen.js:2845 POST https://dev.〇〇.com/wp-admin/admin-ajax.php net::ERR_FAILED scheduleTask @ zone-evergreen.js:2845 scheduleTask @ zone-evergreen.js:385 onScheduleTask @ zone-evergreen.js:272 scheduleTask @ zone-evergreen.js:378 scheduleTask @ zone-evergreen.js:210 scheduleMacroTask @ zone-evergreen.js:233 scheduleMacroTaskWithCurrentZone @ zone-evergreen.js:1134 (anonymous) @ zone-evergreen.js:2878 proto.<computed> @ zone-evergreen.js:1449 (anonymous) @ http.js:2601 _trySubscribe @ Observable.js:42 subscribe @ Observable.js:28 subscribeToResult @ subscribeToResult.js:9 _innerSub @ mergeMap.js:59 _tryNext @ mergeMap.js:53 _next @ mergeMap.js:36 next @ Subscriber.js:49 (anonymous) @ subscribeToArray.js:3 _trySubscribe @ Observable.js:42 subscribe @ Observable.js:28 call @ mergeMap.js:21 subscribe @ Observable.js:23 call @ filter.js:13 subscribe @ Observable.js:23 call @ map.js:16 subscribe @ Observable.js:23 (anonymous) @ Observable.js:91 ZoneAwarePromise @ zone-evergreen.js:960 toPromise @ Observable.js:89 post @ article.service.ts:91 async function (async) noAnimation @ index-e98418f5.js:89 runTransition @ index-e98418f5.js:47 async function (async) runTransition @ index-e98418f5.js:44 (anonymous) @ index-e98418f5.js:15 consumeTimeout @ index-26ec602c.js:2017 flush @ index-26ec602c.js:2041 invoke @ zone-evergreen.js:364 run @ zone-evergreen.js:123 (anonymous) @ zone-evergreen.js:857 invokeTask @ zone-evergreen.js:399 runTask @ zone-evergreen.js:167 drainMicroTaskQueue @ zone-evergreen.js:569 Promise.then (async) scheduleMicroTask @ zone-evergreen.js:552 scheduleTask @ zone-evergreen.js:388 onScheduleTask @ zone-evergreen.js:272 scheduleTask @ zone-evergreen.js:378 scheduleTask @ zone-evergreen.js:210 scheduleMicroTask @ zone-evergreen.js:230 scheduleResolveOrReject @ zone-evergreen.js:847 resolvePromise @ zone-evergreen.js:785 (anonymous) @ zone-evergreen.js:705 step @ tslib.es6.js:73 fulfilled @ tslib.es6.js:71 invoke @ zone-evergreen.js:364 onInvoke @ core.js:41275 invoke @ zone-evergreen.js:363 run @ zone-evergreen.js:123 (anonymous) @ zone-evergreen.js:857 invokeTask @ zone-evergreen.js:399 onInvokeTask @ core.js:41253 invokeTask @ zone-evergreen.js:398 runTask @ zone-evergreen.js:167 drainMicroTaskQueue @ zone-evergreen.js:569 Promise.then (async) scheduleMicroTask @ zone-evergreen.js:552 scheduleTask @ zone-evergreen.js:388 onScheduleTask @ zone-evergreen.js:272 scheduleTask @ zone-evergreen.js:378 scheduleTask @ zone-evergreen.js:210 scheduleMicroTask @ zone-evergreen.js:230 scheduleResolveOrReject @ zone-evergreen.js:847 resolvePromise @ zone-evergreen.js:785 (anonymous) @ zone-evergreen.js:705 webpackJsonpCallback @ bootstrap:25 (anonymous) @ index-index-module-ngfactory.js:1 Show 17 more frames
該当のソースコード
php
1header("Access-Control-Allow-Origin: *");
試したこと
function add_allow_header( $headers ) { global $wp; if (preg_match ('/wp-json/',$wp->request)) { $headers['Access-Control-Allow-Origin'] = '*'; $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf8'; return $headers; } } add_filter( 'wp_headers', 'add_allow_header' );
を追加
WP-APIの「Access-Control-Allow-Origin」をいじった覚書
補足情報(FW/ツールのバージョンなど)
WordPress
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。