跨源请求被阻止:同源策略不允许读取远程资源,CORS 标头中缺少令牌“缓存控制”



我有以下函数,它应该将JSON值传递给外部API:

ack_receipt();
function ack_receipt() {
  var app_name = "Test Trial Account for infobip";
  $.ajax({
    url: "http://api.infobip.com/2fa/1/applications",
    async: true,
    crossDomain: true,
    headers: {
      "authorization": "Basic xxxxxxxxxxxxxxx",
      "cache-control": "no-cache"
    },
    type: 'POST',
    dataType: 'JSON',
    data: {
      "name": app_name
    },
    success: function(data, status) {
      console.log(status);
    },
    error: function(x, status, error) {
      console.log(x, status, error);
      if (x.status == 403) {
        swal("Sorry, your session has expired. Please login again to continue");
      } else if (x.status == 404) {
        swal("Sorry, something went wrong from our side");
      } else {
        console.error("An error occurred: " + status + "nError: " + error);
      }
    }
  });
}

但是,当我尝试从浏览器运行该功能时,我收到以下警告,并且脚本在途中失败:

此站点使用 SHA-1 证书

;建议您使用具有签名算法的证书,这些算法使用比 SHA-1 更强的哈希函数。

阻止跨源请求:同源策略不允许在 https://api.infobip.com/2fa/1/applications 读取远程资源。(原因:CORS 预检通道的 CORS 标头"访问控制-允许标头"中缺少令牌"缓存控制")。

请告知如何处理帖子和Cache-control

同源策略:根据该策略,Web 浏览器允许第一个网页中包含的脚本访问第二个网页中的数据,但前提是两个网页具有相同的来源。https://en.wikipedia.org/wiki/Same-origin_policy

实际上,出于安全原因,现代浏览器不允许跨域访问。

这意味着网页及其尝试加载的 JSON 文件必须位于同一服务器上。

我也面临同样的问题,我的解决方法是发送 通过位于我的系统中的 API 请求(因为浏览器没有 允许跨源,但对节点甚至你来说都不是问题 可以提出卷曲请求)

我的一个朋友也建议创建一个代理服务器,但如果你注意到上面的步骤也像一个代理。

最新更新