I looked on the Internet regarding CORS, and I wanted to confirm what I made of it, that it really is.
Mentioned below is a completely fictional scenario.
I will give an example of a regular website. Say my html page has a form that shows the name of a text field. When submitting data, it sends the form data to myPage.php . Now, what is happening internally, the server sends a request to www.mydomain.com/mydirectory/myPage.php along with the text fields. Now the server sees that the request was fired from the same domain / port / protocol
( Question 1. How does the server know about all these details. Where does it retrieve all this data from? )
However, since the request is created from the same domain, it is a php script server and returns everything that is required of it.
Now, for the sake of argument, let's say I donβt want to manually fill in the data in the text box, but instead I want to do it programmatically. What I do, I create an html page with javascript and run a POST request along with the parameters (i.e. textField Values). Now, since my request is not from any domain as such, the server ignores the service for my request. and i get a cross domain error?
Similarly, I could write a Java program as well that uses an HTTPClient / Post request and does the same.
Question 2: Is this a problem?
Now that CORS gives us, the server says that "anyone can access myPage.php." From enable cors.org it says
For simple CORS requests, the server needs to add the following header to the response: Access-Control-Allow-Origin: *
Now, what exactly is the client going to do with this header. As in the case, did the client somehow want to make a call to resources on the server? He needs to be up to the server to just configure himself whether he wants to accept or not, and act accordingly.
Question 3: How to use sending the header back to the client (who has already made a request to the server)?
And finally, I do not understand that, say, I am creating some RESTful services for my Android application. Now, let's say I have one POST service www.mydomain.com/rest/services/myPost . I have a Tomcat server serving these services on my local machine.
In my Android app, I just call this service and return the result (if any). Where exactly did I use CORS in this case. Does this fall under a different category of server calls? If so, how exactly.
Also, I checked Enable Cors for Tomcat and it says that I can add a filter to my web.xml of my dynamic web project and then it will start accepting it.
Question 4: Is this what allows you to call from my Android device to my web services?
thanks