Shankar Kamble
Shankar Kamble

Reputation: 3023

"No 'Access-Control-Allow-Origin' header is present on the requested resource" error for response from http://www.google.com/

//Create an Angular Module.
var newsModule = angular.module('NewsModule', []);

//Create an Angular Controller.
newsModule.controller('newsCtrl', ['$scope', '$http', function ($scope, $http) {
//function retrives POST,UPDATE,DELETE,GET data

$http.defaults.headers.put = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
        };
        $http.defaults.useXDomain = true;

    $scope.throughdata = function (){

 delete $http.defaults.headers.common['X-Requested-With'];
         $http.get('http://www.google.com').then(function(data,error){
            alert(data);
            alert(error);
            $scope.days=data.data;
          });



    }
}
 ]);

But I have getting following errors

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

Upvotes: 26

Views: 93878

Answers (1)

Khanh TO
Khanh TO

Reputation: 48972

Access-Control-Allow-Origin is set on the response from server, not on client request to allow clients from different origins to have access to the response.

In your case, http://www.google.com/ does not allow your origin to have access to the response. Therefore you cannot read it.

For more information about CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Upvotes: 47

Related Questions