Ionic http get not working

I trying to do a $http GET call from my ionic app . It alway getting me an error.

           $http({
            withCredentials: true,
            method: 'GET',
            url : 'http://google.com'

       }).success(function(data, status, headers, config) {
        console.log("Authentication success" + data);
       })
       .error(function(data, status, headers, config) {
        console.log("Authentication error");
       });

I looked at a code pen example http://codepen.io/pliablepixels/pen/mJxROZ?editors=101 it works perfectly for the https://api.myjson.com/bins/30vuu but if i change it to google.com it fails.

Can someone please help solve this.

Look at your browser console log, you should see this error.

XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access.

So you should learn about CORS before doing so.

Even if you had the CORS headers in your requests, it would probably fail because the code is expecting to hit a JSON API and you’re pointting it to google home page which returns HTML.

1 Like