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

Method 1 of 2:

Client Side Domain

  1. This involves sending a request for some resource
  1. How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery Picture 1
    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. How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery Picture 2
    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. How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery Picture 3
    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. How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery Picture 4
    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. How to Enable Cross Origin Resource Sharing (CORS) for Sharing Resource Using Apache Servers, PHP and Jquery Picture 5
    If the headers are specified correctly, then resource access is granted successfully to client side domain.
4.5 ★ | 2 Vote

May be interested

  • How to Make a PHP Hit CounterPhoto of How to Make a PHP Hit Counter
    hit counters are a popular feature among new and experienced webmasters alike. there are several different methods of including a hit counter on your site, but this manual focuses on a specific implementation in php. it is assumed you are...
  • How to Create and Call PHP FunctionsPhoto of How to Create and Call PHP Functions
    perhaps you have learned the basics of writing php scripts. but sometimes your code may be long and repetitive. php functions are a flexible and easy way to consolidate code. this tutorial will you teach the basics of php functions. (note:...
  • How to Install PHPMailerPhoto of How to Install PHPMailer
    need to send emails from your php application? the default mailing system in php (mail()) doesn't offer the customization features of phpmailer, which is the most popular mail extension for php. this wikihow will show you how to install...
  • How to Write PHP ScriptsPhoto of How to Write PHP Scripts
    php is a server scripting language used to make web pages interactive. it became widely popular due to its ease of use, interactivity within web pages, and integration with html. think of what happens when a page is edited on this website....
  • How to Open a PHP FilePhoto of How to Open a PHP File
    this wikihow teaches you how to open and edit a php programming file on your windows or mac computer. download and install notepad++. notepad++ is a free, windows-only text editor that can open php files. to install it, do the following:
  • Array (Array) in PHPPhoto of Array (Array) in PHP
    an array is a data structure that stores one or more of the same value types in a single value.