Joe Conlin
Joe Conlin

Reputation: 5994

No 'Access-Control-Allow-Origin' with Angularjs

Cannot figure out how to get around cross domain issue. My code:

$apiUrl = 'https://gtmetrix.com/api/0.1/test';
$apiUser = '[email protected]';
$apiKey = '1234567890';
$requestUrl = 'http://converge.io';

    function FetchCtrl($scope, $http, $templateCache) {

        $scope.method = 'post';
        $scope.url = $requestUrl;

        $scope.fetch = function() {
            $scope.code = null;
            $scope.response = null;

            $http({method: $scope.method, url: $apiUrl + '?login-user=' + $apiUser + '&login-pass=34bcb5c46bc6d5fb18a8552820027eb9' + '&url=' + $scope.url, cache: $templateCache}).
            success(function(data, status) {
                $scope.status = status;
                $scope.data = data;
            }).
            error(function(data, status) {
                $scope.data = data || "Request failed";
                $scope.status = status;
            });
        };
        $scope.fetch();

        $scope.updateModel = function(method, url) {
            $scope.method = method;
            $scope.url = url;
        };

    }

The error:

XMLHttpRequest cannot load https://gtmetrix.com/api/0.1/[email protected]&login-pass=1234567890&url=http://converge.io. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://converge' is therefore not allowed access. 

It seems that I need to include the following but cannot get it to work:

delete $http.defaults.headers.common['X-Requested-With'];

Upvotes: 1

Views: 5049

Answers (1)

Philippe Boissonneault
Philippe Boissonneault

Reputation: 3949

I think there is a workaround for this. Here is the logic

  1. Make a little PHP script on your server (or any server side scripting)
  2. Call it with ajax and the api params you need (url, user, key ...)
  3. Use CURL to do the request (or any equivalent)
  4. Print what you need

So instead of calling gtmetrix with your xhttprequest, try calling the script you've done.

Upvotes: 2

Related Questions