CORS: PHP: Response to preflight request doesn't pass. Am allowing origin

CORS: PHP: Response to preflight request doesn't pass. Am allowing origin

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging CORS Preflight Failures and Client Credential Errors

When debugging Cross-Origin Resource Sharing (CORS) issues, it is common to encounter the error: "Response to preflight request doesn't pass access control check." This specific failure mode indicates that the browser blocked the request before your PHP application could even process the logic. However, a closer look at the symptoms reveals a secondary issue that often confounds developers moving from local development to production environments.

In the scenario described, the Angular application hosted at app.example.com fails to communicate with the API at api.example.com. The browser logs show a missing Access-Control-Allow-Origin header during the preflight OPTIONS request. While this is a classic CORS symptom, the underlying PHP code provided attempts to handle credentials and specific headers correctly.

The primary suspect for the initial failure is often how the origin is being set or matched. In your PHP implementation, you are checking $_SERVER['HTTP_ORIGIN']. If this header is empty, or if the casing does not match exactly (though most servers normalize this), the condition fails. However, a critical detail in your configuration is the use of mixed protocols: http://example.com versus https://example.com. Modern browsers enforce strict protocol matching. If your production site serves over HTTPS but your PHP logic allows HTTP origins, or vice versa, the browser will reject the header entirely to prevent downgrade attacks. Ensure that the allowed domains list strictly matches the protocol used by the frontend at runtime.

Beyond the CORS headers, there is a distinct error appearing in Postman and the live site after switching headers to *: "invalid_client" with the description "Client credentials were not found in the headers or body". This is not a CORS issue; it is an authentication logic failure. Your Angular service attempts to send credentials using Basic Auth encoded via btoa(), but the payload string passed to the POST request is hardcoded:

return this.http.post(this.apiBaseUrl + '/Account/Login', "grant_type=client_credentials", { headers: headers })

This code sends a body containing grant_type=client_credentials while simultaneously sending an Authorization header. This creates a contradiction. The OAuth 2.0 specification defines grant_type=client_credentials as a method for resource servers to authenticate clients without user-specific credentials, typically requiring the client ID and secret in the headers or body, not a username/password pair.

If your backend expects Basic Auth (Username/Password), the request body should be empty or omitted entirely. If it expects OAuth 2.0 Client Credentials flow, the Authorization header must contain Basic base64(client_id:client_secret), and the body should usually be empty or strictly formatted according to the RFC. Mixing these two approaches results in the backend rejecting the request because it cannot parse valid credentials from the conflicting inputs.

A common pitfall when moving from local development to production is that local servers (like localhost) often omit the HTTP_ORIGIN header entirely, allowing requests through implicitly or with lax checks. Production servers, however, are stricter. If your PHP logic relies on $_SERVER['HTTP_ORIGIN'] being present to set the allowlist, and your Angular app does not explicitly send the Origin header (which it usually does automatically), you might expect it to work. However, if your server environment strips this header or if the matching logic is too rigid regarding SSL, access is denied.

One effective strategy observed in the source material was moving CORS headers to .htaccess. This decouples the logic from PHP execution flow. Since OPTIONS requests do not execute PHP code by default on many configurations, placing headers in .htaccess ensures they are sent regardless of whether PHP processes the file. However, this does not solve the authentication mismatch.

To resolve this, ensure your Angular request sends the correct credentials format expected by your backend. If using Basic Auth, remove the body payload:

headers.append('Authorization', 'Basic ' + btoa(login.Username + ':' + login.Password));
return this.http.post(this.apiBaseUrl + '/Account/Login', '', { headers: headers }); // Empty body

If you are implementing OAuth 2.0 Client Credentials, ensure the Authorization header uses the client ID and secret, and remove the username/password from the headers. Aligning the authentication method with the expected format will resolve the invalid_client error, while strict protocol matching in your CORS headers will fix the preflight failures.

Our network