使用angular js通过代理服务器发出Http请求

时间:2016-04-06 13:11:31

标签: javascript angularjs cordova proxy

是否可以使用angular $ http通过代理服务器调用Web服务? 我的web服务托管在我的服务器中,我们有一个代理服务器,我可以通过代理重定向我的请求,而不是通过更改设备属性,而是通过代码?

$http({
    method: $scope.method,
    url: $scope.url,
    params: { "member_id": "998014437" }
    })
    .then(function (response) {
        $scope.status = response.status,
        $scope.data = response.data;
    }, function (response) {
        $scope.data = response.data || "Request failed";
        $scope.status = response.status;
    }
); 

1 个答案:

答案 0 :(得分:1)

据我所知,XMLHttpRequest的webproxy设置中没有任何内置版本,因此也没有针对angular $ http的内容。在此问题上使用代理的常见做法是在域上构建服务器端代理页。类似的东西:

"/proxy.php?url=http://crossdomain.com/somedata.json"

此页面基本上在服务器端发出Web请求,并使用标头和所有内容向客户端返回完全相同的响应。然后通过此代理页面提出所有请求。由于代理网址部分位于实际网址的开头,因此您可以使用url: proxyPrefix + $scope.url之类的变量对其进行自定义。如果代理前缀变量为空,则请求将通过实际服务器完成。

相关问题