I have the following subdomain:
example.com
blog.example.com
forum.example.com
ajax.example.com
All the CNAME
aliases are pointing to @
.
The ajax.example.com
is used to make the AJAX calls from any other page, for example from blog.example.com
. There is of course a controller called AjaxController
to handle the requests. Apache mod_headers
is obviously enabled.
In the HTML of blog.example.com
, a click on a anchor tag triggers the following JavaScript code, making an AJAX call.
$(document).ready(
function() {
$("article > div.item-tools > a:first-child").click(
function() {
event.preventDefault();
var postId = $(this).parents("article").attr("id");
$.post('https://ajax.example.com/like/', {id: postId}, function(data) {
$(this).addClass('active');
});
}
);
}
);
In the JavaScript console I see the following error:
XMLHttpRequest cannot load https://ajax.example.com/like/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://blog.example.com' is therefore not allowed access.
XHR instead says:
Remote Address:77.72.53.198:80
Request URL:https://ajax.example.com/like/
Request Method:POST
Status Code:301 Moved Permanently
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,it;q=0.6,es;q=0.4
Connection:keep-alive
Content-Length:39
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:ajax.example.com
Origin:https://blog.example.com
Referer:https://blog.example.com/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Form Dataview sourceview URL encoded
id:46b076f8-81e6-437b-d515-81c03baeecdf
Response Headersview source
Connection:Keep-Alive
Content-Length:238
Content-Type:text/html; charset=iso-8859-1
Date:Tue, 08 Jul 2014 00:47:49 GMT
Keep-Alive:timeout=15, max=100
Location:https://example.com/like/
Server:Apache
I have tried to set Access-Control-Allow-Origin
, because I know it's a problem related to CORS, but doesn't work.
header('Access-Control-Allow-Origin: *');
The fact is that I can't even debug, even if I put a breakpoint on the first line of the bootstrap php file. I have tried to set the Access-Control-Allow-Origin
header in the httpd-vhosts.conf
file, but nothing has changed. Any idea? I just need GET and POST methods, because I'm not making a REST service, and I want avoid JSONP.
What can I try? Where can I put the header function call to make it work? I'm using Phalcon\MVC\Application class and not the Micro version.