InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

HTTP tutorials

  • CORS - cross origin request tutorial in PHP
  • HTTP cookie and sub domains
 
  • Home
  • > Tutorials
  • > Web Development
  • > HTTP

CORS – cross origin request tutorial and example in PHP

By admin | Last updated on Jun 6, 2020

CORS (cross origin resource sharing) can be used to make AJAX requests to different origins. Here are some points to note:
cors-request-header-response-header

  1. CORS un-aware browser will not make a CORS request.
  2. CORS aware browser will make CORS request with following header
    Origin: http://requestingdomain.com
    
  3. Server must respond with the following header for a cross origin request to be honoured by CORS aware browser.
    Access-Control-Allow-Origin: *
    ## or
    Access-Control-Allow-Origin: alloweddomain.com
    

    This way CORS unaware server don’t have to worry about CORS as they will never server this response header.

  4. Requests made from iframe will use parent domain/host for Origin header
  5. CORS is now supported in most modern browsers (Chrome, Forefox, IE11+).

CORS allow same domain example – request from same domain using jQuery

This is default case and everything works fine here. No extra request or reponse header due to CORS.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div id="result1">-</div>
<script type="text/javascript">
$.ajax({
  url: "/demo2/cors/hello_allowed_from_same.php"
})
.done(function( data ) {
  $("#result1").html(data);
});
</script>
refresh done
try it online

CORS allow same domain example – request from different domain using jQuery

Here jQuery adds the following CORS header automatically:

Origin:http://infoheap.com

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div id="result1">-</div>
<script type="text/javascript">
$.ajax({
  url: "https://www.infoheap.com/demo2/cors/hello_allowed_from_same.php"
})
.done(function( data ) {
  $("#result1").html(data);
});
</script>
refresh done
try it online

Since server does not respond with right CORS header, Chrome throws this error in console:

XMLHttpRequest cannot load https://www.infoheap.com/demo2/cors/hello_allowed_from_same.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://infoheap.com' is therefore not allowed access.

Note that here server actually served the request unaware of CORS. It was client Chrome which blocked it after getting response from server.

CORS allow any domain example – request from different domain using jQuery

Here following request header is added by jQuery

Origin:http://infoheap.com

And the following response header by server:

Access-Control-Allow-Origin: *

So everything works fine and result is displayed.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div id="result1">-</div>
<script type="text/javascript">
$.ajax({
  url: "https://www.infoheap.com/demo2/cors/hello_allowed_from_any.php"
})
.done(function( data ) {
  $("#result1").html(data);
});
</script>
refresh done
try it online

Suggested posts:

  1. Requirejs – quickstart guide for beginners
  2. HTML form file upload hello world
  3. Handle XSS restriction using different domain for user entered javascript
  4. CSS general sibling selector
  5. CSS attribute presence and value selectors
  6. CSS – align text in center horizontally
  7. React basic clock example using setInterval
  8. How to make best use of Alexa
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged HTTP, PHP, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress