У меня такая проблема при ajax запросе на другой домен: есть код, который делает запрос на страницу
http://mysite.ru/index.php и методом GET передает параметр view=category
<html>
<head>
<script>
function ajax(){
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (xhttp.readyState == 4 && xhttp.status == 200){
document.getElementById('field').innerHTML = xhttp.responseText;
document.getElementById('field').style.visibility = 'visible';
}
if (xhttp.readyState != 4){
document.getElementById('field').style.visibility = 'hidden';
}
}
xhttp.open('GET', 'http://mysite.ru/index.php?view=category', true);
xhttp.send();
}
</script>
</head>
<body>
<div id="field"></div>
<input type="button" value="Start" onclick="ajax()">
</body>
</html>
Содержимое файла
http://mysite.ru/index.php:
<?php
header("Access-Control-Allow-Origin : *");
header("Access-Control-Allow-Headers: Content-Type, X-Requested-With");
print_r($_GET);
?>
При выполнении запроса в div с id="field" должен выводиться массив GET данных, которые я посылал при запросе, но выводится просто Array ( ), т. е. пустой массив.
То же самое происходит и с POST:
<html>
<head>
<script>
function ajax(){
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (xhttp.readyState == 4 && xhttp.status == 200){
document.getElementById('field').innerHTML = xhttp.responseText;
document.getElementById('field').style.visibility = 'visible';
}
if (xhttp.readyState != 4){
document.getElementById('field').style.visibility = 'hidden';
}
}
xhttp.open('POST', 'http://mysite.ru/index.php', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send('view=category');
}
</script>
</head>
<body>
<div id="field"></div>
<input type="button" value="Start" onclick="ajax()">
</body>
</html>
Содержимое файла
http://mysite.ru/index.php:
<?php
header("Access-Control-Allow-Origin : *");
header("Access-Control-Allow-Headers: Content-Type, X-Requested-With");
print_r($_POST);
?>
И опять выводится пустой массив. В чем может быть проблема?