I get the following error:
XMLHttpRequest cannot load www.mysite/ajax.php No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'www.mysite' is therefore not allowed access. The response had HTTP status code 404.
This occurs when I try to process an Ajax request.
This is fixed by adding:
header('Access-Control-Allow-Origin: http://www.example.com/');
However, this only works when I echo
something (it can be anything) after this line:
<?php
header('Access-Control-Allow-Origin: http://www.example.com/');
echo "<span></span>"; //if this is removed, I'm presented with the error
require_once 'functions.php';
...
?>
When the echo
is emitted, I'm once again presented with the error.
Can anyone explain this peculiar behaviour?
EDIT
Could it have anything to do with the structure of my Ajax call?
var formdata = $( "#calculator_form" ).serialize();
$.get( "ajax.php", formdata). done( function( data ) {
$('.result').html(data); // Printing result into result class div
});
EDIT 2
This can be found in the error_log:
malformed header from script. Bad header=<>... User unknown: php-cgi, referer: http://www.example.com/
NOTE - both ajax.php
and index.php
are on the same domain even though the responses and errors would suggest elsewise.