0

I am trying to read 403 status code(expected) in error call back but I see the following as response object.

Object {data: null, status: -1, config: Object, statusText: "", headers: function}

The preflight OPTIONS request is getting denied with 403. request How can I get the status code '403' and statustext 'Forbidden' in errorcallback?

fileUpload(url,formdata){
  this.$http({
  method: 'POST',
  url: url,
  transformRequest: angular.identity,
  headers: {'Content-Type': undefined}
}).then(function successCallback(response) {

  }, function errorCallback(response) {
    console.log(response);
});
snorkpete
  • 14,278
  • 3
  • 40
  • 57
  • You should white a interceptor for this to handle your errors in response you won't get the status as I know – Babar Hussain Apr 19 '17 at 23:04
  • which angular version are you using? There have been various bugs and fixes related to `response.status` in various angular versions.... – Claies Apr 19 '17 at 23:51
  • for what it's worth, this isn't isolated just to angular, it is really a browser issue. If the server does not return a CORS header (which I assume is the case, since you mention a preflight OPTIONS fail), the browser generally won't parse the XHR object, which means there is no `data`, no `status`, and even no `headers` in the `response`. – Claies Apr 19 '17 at 23:55
  • @Claies Thanks! I am using angular 1.5.11. Do you think using interceptors will work? I believe the response will be the same inside the interceptor as well. Sorry, If I am wrong. Is there a work around that can be done in the server side? – Venkatesh Kandasamy Apr 20 '17 at 00:34
  • The browser is blocking the response because it violates [Same Origin Policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy). To get the browser to relax SOP, the server needs to respond with CORS headers to the pre-flight request. For more information, see [MDN HTTP Reference - HTTP access control (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS). – georgeawg Apr 20 '17 at 06:17

0 Answers0