I have a problem with an AJAX request to other website. When I call Http to Http there is no problem. But When I call from HTTP to HTTPS then I have error message as below:
Access to XMLHttpRequest at 'https://www.mywebsideserver.pl/test.php' from origin 'http://mywebsideclient.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. klientcors.php:77 jquery.min.js:2 POST https://www.mywebsideserver.pl/test.php net::ERR_FAILED
These are the client-side headers.
And this is my client-side code:
<script type="text/javascript">
testCORS();
function testCORS() {
$.ajax({
url: 'https://www.mywebsideserver.pl/test.php',
type: 'POST',
//dataType: 'html',
data:{},
crossDomain: true,
success: function (data, textStatus, xhr) {
alert(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}
</script>
And Server-side easy code:
<?php
header('Access-Control-Allow-Origin: *');
$myArr = array("xx1", "xx2", "xx3", "xx4");
$myJSON = json_encode($myArr);
echo $myJSON;
?>
Something needs to be added to htaccess , client-side code or server-side code ?