How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery

All kinds of interaction on web involve either a client side or a server side. The client side (usually a web browser) launches a request for some resource on server and the server side (usually a PHP or ASP) gives a response back. If the...

Method 1 of 2:

Client Side Domain

  1. This involves sending a request for some resource
  1. Picture 1 of How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery
    Open the HTML file present on the client side in which you are sending resource request. As shown, the client is sending request to server side for getting customer's record with ID as '145'.
  2. Picture 2 of How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery
    Mention the IP address of the server side domain and complete path of the file that need to be accessed. As shown, the file demo.php which needs to be accessed is present on the Apache sever (occupying port 80) of the server side domain. The server side domain has an IP address of 192.148.0.108.
  3. Picture 3 of How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery
    Launch the request using your Apache server by putting the URL localhost/CORS-UsingJquery/sub2.html on the web browser's address bar. As the path indicates, the requesting client side file sub2.html is present inside a folder named as CORS-UsingJquery. If the access headers are properly set on the server side, then the record of the customer with ID as '145' will be successfully displayed.
Method 2 of 2:

Server Side Domain

  1. This involves generating a response to a resource request.
  1. Picture 4 of How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery
    Open the PHP file present on the server side in which you intend to send the response back to the client side.
    1. Set and customize appropriate headers to allow client's access to file demo2php. The statement header ('Access-Control-Allow-Origin: *'); allows access to every domain as indicated by asterisk (*). You can also specify the IP address of some specific domain to grant access only to that domain.
  2. Picture 5 of How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery
    If the headers are specified correctly, then resource access is granted successfully to client side domain.
Update 05 March 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile