JS

JAVASCRIPT CORS FETCH WITH XAMPP

(quick guide & example)

FOR WINDOWS Windows\System32\drivers\etc\hosts

POINT SITE-A.COM & SITE-B.COM TO LOCAL IP ADDRESS 127.0.0.1 site-a.com 127.0.0.1 site-b.com

EDIT HOSTS FILE

01

MAC/LINUX etc/hosts

ADD VIRTUAL HOSTS IN APACHE xampp\apache\conf\extra\httpd-vhosts.conf

VIRTUAL HOSTS

02

<VirtualHost *>   DocumentRoot "C:/sitea/"   ServerName site-a.com </VirtualHost>

<VirtualHost *>   DocumentRoot "C:/siteb/"   ServerName site-b.com </VirtualHost>

CORS FETCH FROM SITE A

03

fetch("http://site-b.com/cors.php", {   mode : "cors",   credentials : "include" }) .then(res => res.text()) .then(txt =>console.log(txt)) .catch(err => console.error(err));

SITE B CORS RESPONSE

04

ALLOW SITE A header("Access-Control-Allow-Origin:   http://site-a.com");

ALLOW CREDENTIALS header("Access-Control-Allow-Credentials: true"); 

OUTPUT echo "It works!";