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

发布于 2024-11-17 08:19:48 字数 3620 浏览 0 评论 0原文

有没有办法在 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>');
    };

Is there any way to set the response header to ("Access-Control-Allow-Origin","*") in jQuery Ajax request, as I am not sure how to do that.. this is my below code.. Any suggestions will be appreciated..

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>');
    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

允世 2024-11-24 08:19:48

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

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

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

You can’t change the response header from client code, it comes from the server.

(You can of course change your request headers (see the jQuery docs for ajax), but that's not going to help here).

If you also have control over what your server sends, then by all means update your post accordingly (i.e. give details of platform / language etc.)

灼疼热情 2024-11-24 08:19:48

“服务器响应标头”表示服务器响应标头,而不是客户端请求标头,因此您无法更改响应标头。您必须将其放在要检索的文件的顶部。

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

"Server response header" states for Server response header, not Client request header so YOU CAN'T CHANGE RESPONSE HEADER. You have to put it at the top of file you're retreiving from.

<?php
header("Access-Control-Allow-Origin: *");
長街聽風 2024-11-24 08:19:48

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

Those headers are for organization websites to communicate with each other. In other words, you have to control all the servers involved. Providing those headers simply relaxes the same-domain restriction for sites with different domain names all owned by the same individual or organization. It does not provide a mechanism for subverting same-domain restrictions with a 3rd-party.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文