as soon as I start the dev-mode and want to execute an ajax-call, for example, I get a cors-error from the browser. I have already tried all variants with the proxy URL and the development environment, but unfortunately I always get this error.
I’ve tested in Chrome and FF, both have different errors:
Chrome
Access to XMLHttpRequest at 'http://local.websitename.de/wp/wp-admin/admin-ajax.php?test=test' from origin 'http://0.0.0.0:3000' 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.
Firefox
QuellĂĽbergreifende (Cross-Origin) Anfrage blockiert: Die Gleiche-Quelle-Regel verbietet das Lesen der externen Ressource auf http://local.websitename.de/wp/wp-admin/admin-ajax.php?test=test. (Grund: CORS-Anfrage schlug fehl). Statuscode: (null)
with wp_localize_data() providing the nonce value and the base URL.
Usage example:
import * as api from './api' // above module
const results = api.fetch('/namespace/v1/endpoint/example');
I think the problem is that you’re requesting the actual site URL.
You want to make a request for localhost:3000/wp-json, because the request is being made from localhost. But, you’re requesting http://local.websitename.de/wp-json. That’s the crux of the problem.
My suggestion is to conditionally use localhost:3000 when in development. There are a great number of ways to do this, but my suggestion is to listen for 'HTTP_REFERER':
$is_dev_request = $_SERVER['HTTP_REFERER'] && strpos($_SERVER['HTTP_REFERER'], 'localhost');
$rest_url = $is_dev_request ? 'http://localhost:3000/wp-json/' : rest_url();
// give this to wp_localize_script
$api_url = esc_url_raw($rest_url)})
You might also try leaving off the base url entirely so that you end up making your requests against /wp-json/... and the browser/lib fills in localhost:3000 for you (or the real URL, in production). I don’t know how your setup will fare with that, but it’s a simple solution if it works.
Out of curiosity - were you using Valet to serve your sites as opposed to a Virtual Box Instance?
I had issues until I added a cors.conf file in the `~/.config/valet/Nginx/’ folder. Sorry to bump an old thread, but thought it may help someone as this was one of the few results that came up when i searched my issues.