HTTPS' GET'在AngularJS中

时间:2015-01-15 12:09:32

标签: javascript angularjs cordova amazon-s3

我正在使用Cordova和AngularJS开发移动应用程序。我正在努力使用AngularJS $ http服务对Amazon S3服务器进行HTTPS“Get”调用。呼叫失败,并显示:

SSLHandshake: Remote host closed connection during handshake

这就是我打电话给S3服务器的方式:

$http({method: 'GET' , url: path});

路径变量是S3的HTTPS网址。如果URL不是HTTPS,这可以正常工作。 在使用Cordova FileTransfer类时我也有这个问题,但是我可以通过在开始下载时将 trustAllHosts 设置为true来解决它。使用AngularJS $ http服务无法做到这一点。

有人知道我怎么解决这个问题吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

在AngularJS中,您应该使用$sce.trustAsResourceUrl(url);来使用HTTPS等安全网址。

示例代码:

$scope.trustSrc = function (path) {
    if (path) {
        if (path.indexOf('https') === -1) {
            path = path.replace('http', 'https');
        }
    }
    return $sce.trustAsResourceUrl(path);
};

这应该返回一个可以在你的GET请求中使用的安全路径,如下所示:

$scope.trustedPath = $scope.trustSrc(path);
$http({method: 'GET' , url: trustedPath});

答案 1 :(得分:0)

    getUser().then(function (response) {
        $scope.users = response.data;
    }).catch(function (err) {
        debugger;
    })

    function getUser() {
        return $http.get("https://jsonplaceholder.typicode.com/users")
    }