我们如何在jQuery Ajax中将响应头设置为Access-Control-Allow-Origin:*

时间:2011-06-24 20:48:35

标签: ajax jquery xmlhttprequest

有没有办法在jQuery Ajax请求中设置响应头(“Access-Control-Allow-Origin”,“*”),因为我不知道该怎么做..这是我的下面的代码。任何建议都将受到赞赏..

var on_show_info = function() {

                request_info = $.ajax({
                url: search_metadata + current_doc_info.id,
                type: 'GET',
                async: true,
                success: on_metadata,
                error: on_metadata_error    
        });
    };

    var on_get_metadata= function(data, text_status, XMLHttpRequest) {

    alert("data");
        console.log(data);
        var html = "";
        var info = {};

        html = html + "<table border ='0'>";
        html= html + "<tr>";
        html =html + "<td><span style='color:#8A8A8A'>Item Location:</span> " +(data.response.docs[0].itemLocation)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>Cycle: </span>" +(data.response.docs[0].lifeCycle)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>Revision: </span>" +(data.response.docs[0].revision)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>Timestamp: </span>" +(data.response.docs[0].timestamp)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>ID: </span>" +(data.response.docs[0].id)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>Attachment: </span>" +(data.response.docs[0].hasAttachment)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>Description: </span>" +(data.response.docs[0].description)+"</td></tr>";
        html =html + "<tr><td><span style='color:#8A8A8A'>Change: </span>" +(data.response.docs[0].changeNumber)+"</td></tr>";
        if(data.response.docs[0].distributionLevel==undefined)
        {
        html =html + "<tr><td><span style='color:#8A8A8A'>Level: </span>Data Not Available</td></tr>";
        }
        else
        {
        html =html + "<tr><td><span style='color:#8A8A8A'>Distribution Level: </span>" +(data.response.docs[0].distributionLevel)+"</td></tr>"; 
        }


        //html =html + "<tr><td><span style='color:#8A8A8A'>Renewal Due Date: </span>" +(data.response.docs[0].c_renewal_due_date)+"</td></tr>";
        if(data.response.docs[0].asicBomSubdescriptor==undefined)
        {
        html =html + "<tr><td><span style='color:#8A8A8A'>descriptor: </span>Data Not Available</td></tr>";
        }
        else
        {
        html =html + "<tr><td><span style='color:#8A8A8A'>descriptor: </span>" +(data.response.docs[0].asicBomSubdescriptor)+"</td></tr>";

        }
        html =html + "<tr><td><span style='color:#8A8A8A'>LifeCycle: </span>" +(data.response.docs[0].lifeCycle)+"</td></tr>";
        html =html + "</table>";


        $('#popup-meta').removeClass('waiting');
        $('#popup-meta .popup-content').html(html);
    };

    var on_get_metadata_error = function(XMLHttpRequest, text_status, error) {
        alert("error");
        $('#popup-meta').removeClass('waiting');
        $('#popup-meta .popup-content').html('<span class="data-error">Unable to get meta data.</span>');
    };

3 个答案:

答案 0 :(得分:3)

您无法更改客户端代码的响应标头,它来自服务器

(您当然可以更改您的请求标头(请参阅ajax的jQuery文档),但这对此没有帮助。)

如果您还可以控制服务器发送的内容,那么请务必相应地更新您的帖子(即提供平台/语言等详细信息)。

答案 1 :(得分:2)

“服务器响应标头”表示服务器响应标头,而不是客户端请求标头,因此您无法更改响应标头。你必须把它放在你要从中恢复的文件的顶部。

<?php
header("Access-Control-Allow-Origin: *");

答案 2 :(得分:0)

这些标题用于组织网站相互通信。换句话说,您必须控制所涉及的所有服务器。提供这些标题只会放松对同一个域或组织拥有的具有不同域名的网站的相同域限制。它提供了一种机制,用于破坏与第三方相同的域限制。