Under the same-origin policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin.
But I did not get the essence of it. If I cannot make a cross domain request from my browser, I will make it through my PHP
script. It will work fine. Isn't it?
So, instead of doing the following:
var xhr = new XMLHttpRequest();
var url = "https://www.google.com/finance/converter?a="+amount+'&from='+from+'&to='+to;
if(xhr) {
xhr.open('GET', url, true);
xhr.onload = function() {
// Handle
};
xhr.send();
}
which will result in :
XMLHttpRequest cannot load https://www.google.com/finance/converter?foo. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin abc.com is therefore not allowed access.
I could do via my own php script by sending a ajax request like:
$.ajax({
method: "GET",
url: "./bin/http/CimmClient.php",
dataType: 'html',
data:{
a: amount,
from: from,
to: to
}
})
.done(function(data, textStatus, jqXHR ){
// Handle
})
And this works perfectly fine. PHP
only sends an HTTP
request to another domain and then sends back the response to javascript.
So, what in principle was the difference? Why does the browser prevent sending cross domain HTTP request, when php/java/others would easily allow this?