我有一个来自远程站点的ajax调用,当我在同一站点上测试它时,当我从远程站点调用它时,我得到200 OK但空响应。
<script type="text/javascript">
function getPage(storename,entries,page) {
$('#output').html('<div class="centriraj"><span>Loading...</span><img id="loader" src="LoaderIcon.gif" /></div>');
jQuery.ajax({
url: "http://jebajiga.byethost32.com/pager.php",
crossOrigin: true,
data:'page='+page+'&entries='+entries+'&storename='+storename,
dataType: "html",
type: "POST",
success:function(data){$('#output').html(data);}
});
}
getPage('cvsstarshop1991',30,1);
</script>
<script type="text/javascript">
function getListCategories(storename) {
jQuery.ajax({
url: "http://jebajiga.byethost32.com/categories.php",
crossOrigin: true,
data:'storename='+storename,
dataType: "html",
type: "POST",
success:function(data){$('#output2').html(data);}
});
}
getListCategories('cvsstarshop1991');
</script>
答案 0 :(得分:1)
为了使用jquery / javascript从远程站点访问数据,远程服务器必须允许CORS。这是一项安全措施。 如果服务器是您的,请将以下内容添加到页眉输出中。
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
}
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);
}