Guruprakash C

Guruprakash C

  • NA
  • 142
  • 27.9k

How to resolve the " access-control-allow-origin " error?

Feb 9 2017 3:49 AM
I came across an error on getting the url meta data by using Angular JS with ajax.

The error statement is:
XMLHttpRequest cannot load http://www.esample.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
 
  1. var app = angular.module('myapp', []);  
  2.   app.controller('ctrl',  
  3.   function ($scope,$http){        
  4.       $http.get('http://example.in/')  
  5.         .success(  
  6.             function(data,status,headers,config){  
  7.             var details = [  
  8.               {title:$(data).filter('title'),  
  9.               description:$(data).filter('meta[name=description]').attr("content"),  
  10.               keywords:$(data).filter('meta[name=keywords]').attr("content"),  
  11.               image:$(data).filter('meta[name=og:image]').attr("content")},  
  12.             ];  
  13.             $scope.details;    
  14.         })  
  15.         .error(function (data, status, headers, config) {  
  16.             $scope.error = " - Something went wrong with your code....";  
  17.         });   
  18. });  
What I have tried:
  1. $http.get('http://example.in/'function (res) {  
  2.               res.header("Access-Control-Allow-Origin""*");  
  3.               res.header("Access-Control-Allow-Headers""X-Requested-With");  
  4. })...  
Reference: access to specific domain only. 

Answers (1)