You remember that all javascript requests are handled by the browser. Thus, the browser detects a cross-origin request.
The request from javascript has nothing to do with PHP / RoR, it is rejected by the browser.
Server code can accept a cross-initial javascript request with the "Access-Control-Allow-Origin" header, because before rejecting the javascript request, the browser will send the "OPTIONS" request to the server to ask the "Access-Control-Allow-Origin" header for a response. If the value matches the current start, the browser will accept a javascript request and send it to the server.
All browsers implement this policy. Same origin policy.
source share