I am trying to get CORS to work on my PHP and it is not working. I only want certain domains to have access to my php file. I have looked at other articles on Stackoverlflow (ie Cross-Origin Request Headers(CORS) with PHP headers), but couldn't get it to work as my $SERVER[ORIGIN] always returns null;
This is my https://dev.mycompany.com/test-cors.php file:
<?
header("Access-Control-Allow-Origin: https://www.mycompany.com");
header("Access-Control-Allow-Methods: GET");
header("Content-Type: application/json; charset=UTF-8");
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
I am trying to call the json.php with ajax from my https://dev.mycompany.com server to see if I would get JSON data back. I was expecting to get an error, but instead got the data retrieved alert message.
Here is my jquery call from the dev server on ajax.js.
$.ajax({
url: "test-cors.php",
type: 'GET',
dataType: 'json', // added data type
success: function(data) {
alert('data retrieved');
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});