0

Info : using AngularJS v1.3.15

EDIT : this issue seems more related to how to handle a 304 response from my webserver rather than ng-file-upload. I change title

I'm trying to manage 304 callback from my webserver and I have the following issue :

TypeError: Cannot read property 'data' of undefined
    at ng-file-upload.js:79
    at angular.min.js:112
    at n.$get.n.$eval (angular.min.js:126)
    at n.$get.n.$digest (angular.min.js:123)
    at n.$get.n.$apply (angular.min.js:126)
    at l (angular.min.js:81)
    at M (angular.min.js:85)
    at XMLHttpRequest.F.onload (angular.min.js:86)

Line 79 of ng-file-upload.js is the following :

promise.success = function (fn) {
    promise.then(function (response) {
       console.log(response,config); // HERE RESPONSE IS UNDEFINED
        fn(response.data, response.status, response.headers, config);
    });
    return promise;
};

My code to upload is the following : This code works well when my WebService sends a 200 response.

       Upload.upload({
            url: $localStorage.endPointVersion + "sky/!/avatar",
            fields: {},
            file: file
        }).progress(function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
        }).success(function (data, status, headers, config) {
            console.log('file ' + config.file.name + 'uploaded. Response: ', data);
        }).error(function (data, status, headers, config) {
          console.log('error ' + config.file.name + 'uploaded. Response: ' + data);
        });

When file already exists on webServer,then, my webservices sends a 304 response.

So far, I have developped some interceptors and saw that 304 is indeed processed into responseError.

$httpProvider.interceptors.push(['$location', '$injector', '$q', function($location, $injector, $q) {
                         return {
                            'responseError': function(rejection) {
                                 $log = $log || $injector.get('$log');

                                  if (rejection.status === 304) {
                                     // I have tried to reject it to go into error of upload, without success
                                     return $q.reject(rejection);

                                 }
                             }
                         };
                     }]);

First question : Is this response code normal ? correct ? if not, what shall be an appropriate response to handle a "no change / not modified" POST ? Changing on Webserver from 304 response to another is possible but has huge impacts. If possible, I'd like to prevent changing that and manange on webapp 304 response properly Second question (and the principal) : How shall I manage this 304 response from my Webserver ?

I have seen that point : $http returns error when response is 304 (Not Modified) in angularjs saying that angular valid status are return 200 <= status && status < 300;

Community
  • 1
  • 1
aorfevre
  • 5,034
  • 3
  • 21
  • 51

1 Answers1

0

I have ended up changing my backend response to handle differently the code status.

This question is solved.

aorfevre
  • 5,034
  • 3
  • 21
  • 51