0

信息:使用 AngularJS v1.3.15

编辑:这个问题似乎与如何处理来自我的网络服务器的 304 响应而不是 ng-file-upload 更相关。我改标题

我正在尝试从我的网络服务器管理 304 回调,但我遇到了以下问题:

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)

ng-file-upload.js 的第 79 行如下:

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;
};

我要上传的代码如下:当我的 WebService 发送 200 响应时,此代码运行良好。

       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);
        });

当 webServer 上已经存在文件时,我的 web 服务会发送 304 响应。

到目前为止,我已经开发了一些拦截器,看到304确实被处理成了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;

4

1 回答 1

0

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

This question is solved.

于 2015-06-09T12:49:46.073 回答