0

I tried to call an external API written in java spring with cors enabled from my angularjs app but i get 403 forbidden error while trying to make a request to the server.

My angularjs controller

.controller('loginCtrl', function($http, $httpParamSerializerJQLike) {
  var vm = this;
  $http.defaults.headers.common['Authorization'] = 'Basic ' + 'd2ViX2FwcDo='
  vm.fnlogin = function(item) {

    var postData = {

      'username': item.email,
      'password': item.password,
      'grant_type': 'password'
    }
    $http({
      url: 'http://sample.herokuapp.com/oauth/token',
      method: 'POST',
      data: $httpParamSerializerJQLike(postData),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }).then(function() {

    })

  }
})

I am getting the below errors

Failed to load resource: the server responded with a status of 403 (Forbidden)

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

Any help will be really appreciated and i am using angular 1.6.4

P.s : If i make a request using postman then i am able to generate oauth tokens but when i call from my angular app i get error. Thanks

Melvin
  • 877
  • 3
  • 11
  • 27
  • Try this answer, I think it will help https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource and maybe this as well https://stackoverflow.com/questions/23823010/how-to-enable-cors-in-angularjs – maydawn Jul 10 '17 at 20:30
  • @maydawn any similar solutions available for angular because i tried most of 'em.The one you gave is for ajax request ? – Melvin Jul 10 '17 at 20:31
  • Try adding this header: 'Content-Type': 'application/x-www-form-urlencoded' to the request. I'm sadly not very knowledgeable on the subject of CORS – maydawn Jul 10 '17 at 20:36
  • @maydawn already did that see the controller code which i posted above – Melvin Jul 10 '17 at 20:38
  • Is CORS enabled on the server ? – maydawn Jul 10 '17 at 20:38
  • @maydawn yes since it works with postman i am sure it is enabled but when i make calls from my angular app i get the error – Melvin Jul 10 '17 at 20:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148822/discussion-between-melvin-and-maydawn). – Melvin Jul 10 '17 at 20:39
  • @Melvin You should enable cors in java web api to solve problem – hasan Jul 10 '17 at 20:40
  • @hsnbl i did that and its working fine when i call from a rest client tool but it fails when i do so from my web app – Melvin Jul 10 '17 at 20:41
  • It will work fine with postman and fiddler even cors is not enabled in web api you are calling this web api method with xhr and it needs to enabled cors – hasan Jul 10 '17 at 20:44
  • You could try these methods to enable CORS in Spring Boot https://stackoverflow.com/questions/40286549/spring-boot-security-cors – maydawn Jul 10 '17 at 20:48

0 Answers0