I think because you’re using application/x-www-form-urlencoded (as opposed to application/json; are you sure you don’t want json?) your request is a simple request and therefore doesn’t trigger a preflight check. You don’t need an authorisation header or mode to make a simple request so you can remove them.
With application/x-www-form-urlencoded form data is sent as a long query string so you can use query parameters like so: https://external_domain/api?name=ferret&color=purple
Thank you for your reply. Actually, I tried to using application/json before but also fail.
Btw, the external AP requires ‘authorisation’ (token) in the request header and it is not allowed to use query parameters to pass the authorization (token). Thanks!
I shared the coding in the post. What other information you needed? Error Message?
If yes, the return message is as follows:
Request Method: GET
Status Code: 401
Remote Address: <external_api_IP>:443
Referrer Policy: no-referrer-when-downgrade
I don’t know why no CORS error today (I have NOT updated my coding). But the custom request headers are still cannot pass to external API. I am sure that my login information is correct.
I think we need to know what kind of authorization the API is asking for. I imagine it’s an API key, not Bearer authentication, so try the x-api-key first (delete the other one):
mode: "no-cors" only allows a limited set of headers in the request: Accept, Accept-Language, Content-Language, and Content-Type. Hopefully the above works.
“Access to fetch at ‘https://external_domain/api’ from origin ‘https://hubspot-demo.mydomain.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’.”
@piersg From your reply - “And what do you get if you use “credentials” instead of “mode”?” , you suggested to change “credentials” to “mode”, right? If not, what do you mean? Could you post the whole coding? Thanks!
Updated the coding as your coding, and return the below error:
Access to fetch at “https://external_domain/api” from origin ‘https://hubspot-demo.mydomain.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’.
Ok, that would suggest the Access-Control-Allow-Origin is set to wildcard (*) i.e. code from any origin can access the API. As it states, you can’t have credentials when that is set to wildcard. What do you get if you remove credentials?
Well 204 error is saying that the server has successfully fulfilled the request and that there is no content to send in the response payload body. So your code works, but there’s nothing being returned from the API.