我需要从位于不同位置的一个文件中获取数据url.Here是显示发送到服务器的ajax请求的代码
<script>
alert('return sent');
$.ajax({
type: "POST",
url: "example.com/show.php",
data: 1
})
success: function data(response) {
alert(data); // apple
}
</script>
下面是我通过Ajax访问的另一个文件(example.com/show.php)上的代码。
<?php
echo 'Hello ';
?>
然而,我在我的控制台中得到了跨域请求被阻止:警告。
发布于 2017-01-18 20:46:37
请像这样回复你的show.php
<?php
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type, *");
echo 'Hello ';
?>
希望能有所帮助
发布于 2017-01-18 20:39:11
在show.php中添加
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type, *");
https://stackoverflow.com/questions/41719595
复制相似问题