【问题标题】:Ajax Angular SSL CORSAjax Angular SSL CORS
【发布时间】:2016-08-19 14:23:25
【问题描述】:

我其实不明白这个问题。我不太喜欢 SSL 和证书。

test.kanubox.de 上的一个脚本(您可以在那里尝试并查看源代码)使用 ajax 调用 sandbox.api.kehrwasser.com/kanubox/v1 上的 rest 服务器。显然 CORS 是必需的,并且在没有 SSL 的情况下运行良好,因此我假设 CORS 设置正确。对 API 的 OPTIONS 请求(预检)的标头数据确认

Access-Control-Allow-Origin: *
Upgrade: h2,h2c
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Content-Type: application/json
Content-Encoding: gzip
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
Server: Apache/2.4
Expires: Fri, 19 Aug 2016 12:15:58 GMT
Access-Control-Max-Age: 500
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, accept, client-security-token, Access-Control-Allow-Origin, X-Frame-Options

但是当我切换到 https://test.kanubox.de 并在 https://sandbox.api.kehrwasser.com/kanubox/v1 调用 API 时,我从 FireFox 收到 CORS 错误,例如“(跨源被阻止)

Reason: CORS-Header 'Access-Control-Allow-Origin' missing

(翻译错误信息)

证书来自我的托管商并由我的托管商自己验证。我不确定,但那是“自签名”吗?那么也许 FF 阻止它是因为它不信任它?

这是我的代码:

  var test = angular.module("test", []);

  test.constant('apiConfig', {
      apiUrl: "https://sandbox.api.kehrwasser.com/kanubox/v1"
  });

  test.controller("TestController", function($scope, $http, apiConfig) {

      var credentials = { mail: "user@mailserver.com", password: "12345" };

      // POST REQUEST VIA SSL
      $http({
          url: apiConfig.apiUrl + "/users/auth/",
          method: 'POST',
          data: credentials
      }).success(function(data, status, headers, config) {

          $scope.variable = data;

      }).error(function(data, status, headers, config) {

          $scope.variable = data;

      });

  });

【问题讨论】:

  • 所有感兴趣的人:问题出在 Firefox 内部。一个特定的版本有这个问题。在早期或更高版本以及其他浏览器中,没有问题。我只是问了用户在这个特定的浏览器版本中更新浏览器。

标签: javascript angularjs ajax ssl xmlhttprequest


【解决方案1】:

如果我浏览到https://test.kanubox.de/,则服务器证书在我的 Firefox 浏览器中是未知的。它是由“Hostpoint DV SSL CA - G2”本身颁发的自签名证书!

要使该 SSL 证书正常工作,您需要在浏览器中使用“Hostpoint”根证书。这正是你让它工作的方式!所以,这是一个受信任的 ROOT 证书问题。

当 SSL 问题解决后,您可以查看 CORS 问题。

https://sandbox.api.kehrwasser.com/kanubox/v1/ 中使用的证书是由著名的 CA “Let's incrypt” 颁发的。效果很好。

【讨论】:

  • 感谢您的回答。我现在看到这个问题并理解它。谢谢。
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2018-06-29
  • 2020-10-08
  • 2014-03-08
相关资源
最近更新 更多