Jquery ajax 跨站

Jquery ajax cross site

我有一个来自远程站点的 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>

为了使用 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);
}