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

The question:

//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'];


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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment