1

我有以下函数应该将 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 标头“Access-Control-Allow-Headers”中缺少令牌“缓存控制”)。

请就如何处理帖子和Cache-control.

4

1 回答 1

0

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

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

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

我也遇到了同样的问题,我的解决方法是通过位于我系统中的 API 发送请求(因为浏览器不允许跨域,但对于节点来说不是问题,甚至你可以发出 curl 请求)

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

于 2018-09-03T13:22:57.567 回答