title: Understanding CORS
body: <p>I've been looking on the web regarding CORS, and I wanted to confirm if whatever I made of it is, what it actually is.</p>
  
  <p><strong>Mentioned below is a totally fictional scenario.</strong></p>
  
  <p>I'll take an example of a normal website. Say my html page has a form that takes a text field name. On submitting it, it sends the form data to <code>myPage.php</code>. Now, what happens internally is that, the server sends the request to <code>www.mydomain.com/mydirectory/myPage.php</code> along with the text fields. Now, the server sees that the request was fired off from the same domain/port/protocol</p>
  
  <p>(<strong><em>Question 1. How does server know about all these details. Where does it extract all these details froms?</em></strong>)</p>
  
  <p>Nonetheless, since the request is originated from same domain, it server the php script and returns whatever is required off it.</p>
  
  <p>Now, for the sake of argument, let's say I don't want to manually fill the data in text field, but instead I want to do it programmatically. What I do is, I create a html page with javascript and fire off a POST request along with the parameters (i.e. values of textField). Now since my request is not from any domain as such, the server disregards the service to my request. and I get cross domain error? </p>
  
  <p>Similarly, I could have written a Java program also, that makes use of HTTPClient/Post request and do the same thing. </p>
  
  <p><strong><em>Question 2 : Is this what the problem is?</em></strong> </p>
  
  <p>Now, what CORS provide us is, that the server will say that 'anyone can access myPage.php'. 
  From <a href="http://enable-cors.org/server.html">enable cors.org</a> it says that </p>
  
  <blockquote>
    <p>For simple CORS requests, the server only needs to add the following header to its response:
    Access-Control-Allow-Origin: *</p>
  </blockquote>
  
  <p>Now, what exactly is the client going to do with this header. As in, the client anyway wanted to make call to the resources on server right? It should be upto server to just configure itself with whether it wants to accept or not, and act accordingly. </p>
  
  <p><strong><em>Question 3 : What's the use of sending a header back to client (who has already made a request to the server)?</em></strong></p>
  
  <p>And finally, what I don't get is that, say I am building some RESTful services for my android app. Now, say I have one POST service <code>www.mydomain.com/rest/services/myPost</code>. I've got my Tomcat server hosting these services on my local machine.</p>
  
  <p>In my android app, I just call this service, and get the result back (if any). Where exactly did I use CORS in this case. Does this fall under a different category of server calls? If yes, then how exactly. </p>
  
  <p>Furthermore, I checked <a href="http://enable-cors.org/server_tomcat.html">Enable Cors for Tomcat</a> and it says that I can add a filter in my web.xml of my dynamic web project, and then it will start accepting it. </p>
  
  <p><strong><em>Question 4 : Is that what is enabling the calls from my android device to my webservices?</em></strong></p>
  
  <p>Thanks</p>