How do I enable cross-origin resource sharing on XAMPP?

How do I enable cross-origin resource sharing on XAMPP?

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your Local XAMPP Setup Blocks Cross-Origin Requests

You are hitting a classic browser security boundary known as CORS (Cross-Origin Resource Sharing). The error message you are seeing—XMLHttpRequest cannot load... No 'Access-Control-Allow-Origin' header is present—is the browser explicitly blocking your code to protect your application from potential attacks.

The core issue isn't just about adding a single line of PHP code; it is about understanding where that code needs to live and how XAMPP handles headers globally versus per-file.

The Header Must Live in the Response, Not the Config

You correctly identified that header("Access-Control-Allow-Origin: *"); is required. However, placing this inside your Apache configuration file (httpd.conf) or a .htaccess file will not work for dynamic requests from JavaScript.

Apache headers set in the config apply to the physical HTTP response generated by the server engine. When you make a request via jQuery (or fetch/XMLHttpRequest), the browser sends an Origin header. For the browser to allow the response, your PHP script must intercept that request and dynamically send back the Access-Control-Allow-Origin header in the specific HTTP response for that request.

If you are hosting the PHP file on a different domain (like www.example.com) but running your HTML form on localhost, the server at example.com is responsible for sending the header. You cannot force the local XAMPP server to accept headers from an external domain without configuring the remote server correctly.

The 404 vs. CORS Distinction

Your initial error log showed a 404 Not Found status alongside the CORS warning. This suggests your browser attempted the request, received a 404, and because the response lacked security headers (or because the 404 was malformed), it threw the CORS error.

When you later tested with var testXHR = $.post(...), you saw data in the alert. This implies the server is responding successfully now. If the CORS header is present on the remote server (www.example.com) and the file exists, the browser will allow the data to pass through. The "failure" you experienced earlier was likely a combination of the missing header and a routing issue that has since been resolved.

Handling Local Development vs. Production

There is a critical architectural distinction you must make here. CORS restrictions are enforced by the client browser based on the Origin header sent by the page making the request.

  1. Remote Server (www.example.com): If your production site is live, it needs to send Access-Control-Allow-Origin. You should restrict this to specific domains rather than using * in production for security best practices (e.g., Access-Control-Allow-Origin: https://yoursite.local).
  2. Local Development (localhost): When developing locally, you often have two scenarios:
    • Scenario A: Both your HTML and PHP are on localhost. In this case, there is no cross-origin issue. The browser considers them the same origin. You do not need to set CORS headers.
    • Scenario B: Your HTML is on localhost, but you are testing against a remote server (www.example.com). This is where CORS triggers.

Implementing the Fix in PHP

To enable this correctly, you must edit the PHP file located at lookup_update.php on your remote server. Add the header logic to the very top of the script, before any output is sent (even a single space or newline can break headers).

<?php
// Enable CORS for all origins (Use specific domains in production)
header("Access-Control-Allow-Origin: *");

// Handle preflight requests
header("Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");

// Your existing logic here
$data = ...; 
echo json_encode($data);
?>

Note the inclusion of OPTIONS handling. Modern browsers often send a preflight OPTIONS request before sending the actual POST. While simple scripts might handle this automatically, robust servers should explicitly allow these headers.

The "Localhost" Caveat

If you are trying to bypass this entirely for local development because setting up a remote server is cumbersome, remember that localhost requests from localhost files never trigger CORS errors. Browsers treat all subdomains and ports of the same host as part of the same origin unless explicitly configured otherwise.

If you need to test cross-origin behavior locally without an external domain, you can use browser extensions like "Allow CORS: Access-Control-Allow-Origin" or run your PHP server using php -S localhost:8000 from a different port than your HTML file if they are served by different means (though this is rarely necessary for standard XAMPP setups).

The solution remains consistent: the header must be injected into the HTTP response of the target resource, not configured globally on the web server.

Our network