From the message:
Failed to load … Origin ‘http://localhost:8000’ is …
…that looks like you were accessing your page on localhost as “http://localhost:8000/ {pageThatCallsCH}” where the last part is obviously the path to your local page launching the javascript you’ve shown above. So the browser then informs the CH server that “localhost” port 8000 is trying to access it, which is not in the CH allowed sites list.
What happens if you try accessing this page by entering the alias in the browser e.g. “http://application.com/ {pageThatCallsCH}” (add the 8000 port if needed)?
Another point is that the options you show (in fetchOptions) don’t mention a “credentials” flag - when I checked:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
… this seemed to state you needed this option (as the “Authorization” header counts, I think):
To cause browsers to send a request with credentials included, even for a cross-origin call, add credentials: ‘include’ to the init object you pass to the fetch() method.
So you may need:
const fetchOptions = {
headers: {
Authorization: 'Basic ' + btoa('API_KEY:')
},
mode: "cors",
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "include" // include, *same-origin, omit
};
More info on CORS: Cross-Origin Resource Sharing (CORS) - HTTP | MDN
Test CORS: http://www.test-cors.org/
I’m not sure about ports - I believe the usual (80 / 443) ports are recognised by Companies House (and 8080?), 8000 is common enough but I don’t know if that’s OK with CH. You could try adding this to the CH allowed URLs if you still have issues (as in the Angular Local Dev Testing thread mentioned previously).
Assuming that ports are the issue and the above didn’t help there are various ways around e.g. like that listed here:
You can add a /etc/hosts entry like the following:
127.0.0.2 my-domain.com
Make sure to use a lo address unused before.
Then you add an iptables rule to redirect the traffic incoming into 127.0.0.2:8080 to 127.0.0.1:80.
iptables -t nat -A OUTPUT -d 127.0.0.2 -p tcp --dport 80 -j REDIRECT --to-port 8080
Or (alternatively) - see last answer here:
You can make localwebapp as alias for localhost in /etc/hosts. Then you can run a webserver (Apache and friends) to detect that hostname:
< VirtualHost *:80 >
ServerName localwebapp
# redirect elsewhere
Redirect localhost:1234
< /VirtualHost >