没有“访问控制允许来源”标头错误,但无论如何开机自检都会通过

No 'Access-Control-Allow-Origin' header error yet the POST goes through anyway

本文关键字:无论如何 开机自检 错误 访问控制 没有      更新时间:2023-09-26

所以,我有这个JavaScript函数,它执行一个简单的jQuery AJAX()请求:

function impLeads() {
    var go_health_id = $(".sync-action").data("subscriber");
    var customer_number = $(".sync-action").data("customer-number");
    var lead_type = $(".sync-action").data("lead-type");
    var person = {
        lead_type: lead_type,
        customer_number: customer_number,
        subscriber_id: go_health_id,
        first_name: "Daniel",
        last_name: "Endo",
        phone: "(937) 555-5555"
    }
    $.ajax({
    type: "POST",
    crossDomain: true,
    dataType: "json",
    data: person,
    url: "https://www.brokeroffice.com/leads/leadImport.do",
    cache: false,
    success: function(html) {
            $(".debug").show().html(html);
            console.log('Leads imported for ' + go_health_id);
        }
    });
}

现在。。。这是发给 BrokerOffice.com 的HTTP帖子。潜在客户数据可以通过HTTP POST导入到BrokerOffice。终结点 URL = https://www.brokeroffice.com/leads/leadImport.do 。我正在从http://mycompanysite.com/leads/执行此脚本。请注意,他们有https://www,而我的没有。

但是,当我进入 BrokerOffice.com 时,我看到潜在客户已成功发布到他们的数据库中,因此......尽管有此警告:

XMLHttpRequest cannot load https://www.brokeroffice.com/leads/leadImport.do. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mycompanysite.com' is therefore not allowed access.

请求通过。

问题是我必须多次循环请求,以便 JavaScript 错误将阻止循环继续执行。

我能做什么?

该标头只会阻止您看到来自服务器的响应(而不是发送请求)。

问题是我必须多次循环这个请求,所以 该 JavaScript 错误将阻止循环继续 执行。

我能做什么?

你能做什么?需要更改来自服务器的响应标头以允许跨源通信。如果您无权更改服务器,那么您可能不走运。

正如@apsillers提到的,您可以发送请求,它们就会到达那里。你只是无法得到回应。

如果需要阅读响应,可以:

  1. 使用轻型 CORS 代理,例如服务器上的 https://github.com/gr2m/CORS-Proxy。
  2. 使用托管代理,例如 http://crossorigin.me/