Angular js通过标头获取请求 - 显示CORS错误

时间:2017-03-09 09:46:15

标签: angularjs http get http-headers

在我的角度控制器代码中,我的http get方法不使用标题但是在postman中使用相同的请求

$http({
   method: 'GET',
   url: 'http://localhost:8080/api/profiles',
   headers: {
     'Authorization': 'Bearer 78954642-sf25-4sd6-sdf2-99582369d5af',
     'Content-Type':'text/plain'
    }
   }).then(function(success) {
     console.log(success);
  }, function(error) {
  });

显示以下错误

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://quotebuilder' is therefore not allowed access. The response had HTTP status code 401.

我的请求标题

OPTIONS /api/profiles HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: GET
Origin: http://quotebuilder
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Referer: http://quotebuilder/login
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8

这里有什么问题?请帮我解决问题

1 个答案:

答案 0 :(得分:2)

您应该在服务器端禁用CORS才能使其正常工作。当资源从不同的域或端口请求资源时,资源会发出跨源HTTP请求,而不是第一个资源本身所服务的资源,请参阅dtails here。如果您使用的是spring或其他服务器端Web框架,则需要从那里禁用它。