4

Is it possible to call a web service though a proxy server using angular $http? My webservices is hosted in my server and we have a proxy server is it possible that i can redirect my request through that proxy not by changing device property but by through code?

$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;
    }
); 
Danscho
  • 466
  • 5
  • 20
Vaisakh N
  • 780
  • 1
  • 11
  • 27

1 Answers1

1

As far as I know there aren't any build in webproxy settings for XMLHttpRequest, so there aren't for angular $http too. The common practice for using a proxy on this matter is that you build a server side proxy page on your domain. Something like:

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

This page basically make the web requests on server side and return the exactly same response to client with headers and everything. And then you make all your requests via this proxy page. Since the proxy url part is at the beginning of your actual url, you can customize this with a variable like url: proxyPrefix + $scope.url . If proxy prefix variable is empty, then the request will be done over the actual server.

Noldor
  • 1,122
  • 9
  • 22
  • Hi Noldor thanks for the answer, I was also come to that there aren't any build in webproxy settings for XMLHttpRequest , Actually I was using this inside a cordova android project so I made a workaround by setting proxy for webview :) like http://stackoverflow.com/questions/4488338/webview-android-proxy – Vaisakh N Aug 11 '16 at 11:50