Access to XMLHttpRequest at 'veri alan site' from origin 'veri gönderen site' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Javascript ile bir siteden başka bir siteye bir post gönderiyorum kodlarım:
function sendmessage() {
var datagel = document.getElementById("usernamee");
var data = datagel.innerHTML;
// POST isteğini göndermek için XMLHttpRequest objesi oluştur
var xhr = new XMLHttpRequest();
// POST isteğinin yapılacağı URL belirt
xhr.open("POST", "burayaurl yazdım", true);
// HTTP header'ına content-type ekle
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// POST isteği yap
xhr.send("k_key=" + encodeURIComponent(data));
// POST isteğinin yanıtı alındığında çalışacak fonksiyon
xhr.onload = function() {
// Eğer yanıt "success" ise aaa.com'a yönlendir
if (this.responseText === "success") {
window.location.href = "buraya da url yazdım";
alert("basarili");
}
else{
alert("yanlıs key");
}
};
}
Diğer siteden bunu php ile alıyorum kodlar çok uzun fakat cors için ekle dedikleri(stackoverflow) kodları ekledim:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}