Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

like image 767
Shankar Kamble Avatar asked Jun 07 '14 13:06

Shankar Kamble


People also ask

How do I fix not allowed by Access-Control allow origin?

In that case you can change the security policy in your Google Chrome browser to allow Access-Control-Allow-Origin. This is very simple: Create a Chrome browser shortcut. Right click short cut icon -> Properties -> Shortcut -> Target.


1 Answers

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

like image 118
Khanh TO Avatar answered Sep 21 '22 08:09

Khanh TO