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

Http request through a proxy server using angular js

本文关键字:代理服务器 请求 Http angular js 使用      更新时间:2024-01-21

是否可以使用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;
    }
); 

据我所知,XMLHttpRequest没有任何内置的webproxy设置,所以也没有angular$http。在这个问题上使用代理的常见做法是在域上构建一个服务器端代理页面。类似于:

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

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