【问题标题】:Api-Version header is not sent correctlyApi-Version 标头未正确发送
【发布时间】:2019-04-29 09:17:12
【问题描述】:

亲爱的朋友们,我有一个奇怪的问题,我尝试连接到 API,当我尝试在 postman 或 insomnia 中执行 API 请求时,一切正常。当我在我的网站甚至本地主机上使用相同的代码时,潜在客户请求不起作用并告诉我未定义 api-version。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "https://affiliate-api.tradingcrm.com:4477/token",
        "method": "POST",
        "data": "{ userName: \"alpt\", password: \"Alpt@12345\" }"
    }

    $.ajax(settings).done(function (response) {
        var settings2 = {
            "async": true,
            "crossDomain": true,
            "url": "https://affiliate-api.tradingcrm.com:4477/accounts/lead",
            "method": "POST",
            "headers": {
                "Authorization": "Bearer " + response.Token,
                "Api-Version": "3",
                "Content-Type": "application/json"
            },
            "data": "{firstName:\"test\",lastName:\"test2\",email:\"test@test.test\"}"
        }
        
        $.ajax(settings2).done(function (response2) {
            console.log(response2.accountId);
        });
    });
</script>

【问题讨论】:

  • "Api-Version" 我不认为这是一个有效的标题
  • 这是一个有效的标题,如果你在 Insomnia 或 postman 中测试这个确切的标题,你会得到非常好的结果。
  • 您是否检查了请求标头上开发人员工具中的网络选项卡?也许 jQuery 会因为您的设置过滤掉您的标题(只是建议)。
  • 很可能Api-Version 标头应该是X-Api-Version,但仅凭源代码无法确定
  • affiliate-api.tradingcrm.com:4477/swagger/# 这是我的 API 手册的招摇,用户名和密码在我提供用于测试的源代码中

标签: javascript jquery api postman


【解决方案1】:

它的CORS 问题,API 服务器缺少标头Access-Control-Allow-Headers,因此您无法设置自定义请求标头,例如"Api-Version": "3",解决方案使用 CORS 代理或从您的服务器检索数据或要求您的提供商添加它。

与 Postman 的不同之处在于 postman 不询问服务器 API,它只是简单地发送标头,而 Browser 因为不同的域它会通过在发出 Post 请求之前使用以下标头执行 OPTIONS 请求来询问。

Access-Control-Request-Headers: api-version,authorization,content-type

那么Server API需要响应headers

Access-Control-Allow-Headers: api-version,authorization,content-type

CORS 代理测试:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://affiliate-api.tradingcrm.com:4477/token",
    "method": "POST",
    "data": "{ userName: \"alpt\", password: \"Alpt@12345\" }"
  }

  $.ajax(settings).done(function(response) {
    console.log('requesting using CORS Proxy.....');
    var settings2 = {
      "async": true,
      "crossDomain": true,
      "url": "https://cors-anywhere.herokuapp.com/https://affiliate-api.tradingcrm.com:4477/accounts/lead",
      "method": "POST",
      "headers": {
        "Authorization": "Bearer " + response.Token,
        "Api-Version": "3",
        "Content-Type": "application/json"
      },
      "data": "{firstName:\"test\",lastName:\"test2\",email:\"test@test.test\"}"
    }

    $.ajax(settings2)
    .done(function(response2) {
      console.log(response2.accountId);
    })
    .fail(function(jqXHR, textStatus) {
      console.log(textStatus);
      console.log(jqXHR.responseText);
    });
  })

</script>

【讨论】:

  • API 服务器还是我尝试运行的服务器?因为所有 API 测试应用程序和网站都可以正常运行代码,如果是 API 提供者服务器问题,它们也无法运行。
  • 我的意思是API服务器,应用程序与浏览器不同,非常受限。
  • 是否有任何解决方法可以从我这边而不是服务器端解决这个问题。
  • 除了我上面所说的或联系您的 API 提供商之外没有其他修复方法
  • @AliMahmoudi — 查看控制台。它应该告诉您 CORS 权限已被拒绝,因为对预检 OPTIONS 请求的响应在需要 200 OK 状态时带有 405 Method Not Allowed 状态。
猜你喜欢
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 2019-04-11
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多