2016-08-11 76 views
0

我刚刚将控制器的上传功能(它正在工作的地方)移动到工厂,并且它突然停止工作。我不断收到此错误,但我不知道/明白问题出在哪里Http请求配置url必须是一个字符串。收到:undefined

angular.js:13550 Error: [$http:badreq] Http request configuration url must be a string. Received: undefined 
http://errors.angularjs.org/1.5.5/$http/badreq?p0=undefined 
    at angular.js:68 
    at $http (angular.js:11194) 
    at uploadWithAngular (ng-file-upload.js:91) 
    at sendHttp (ng-file-upload.js:144) 
    at upload (ng-file-upload.js:330) 
    at Scope.$scope.uploadDocument (locationsCtrl.js:131) 
    at fn (eval at compile (angular.js:14432), <anonymous>:4:338) 
    at expensiveCheckFn (angular.js:15485) 
    at callback (angular.js:25018) 
    at Scope.$eval (angular.js:17229) 

这是在我的控制文件上传功能

$scope.uploadDocument = function(file) { 
    if($scope.documentName.length > 4) { 
     $scope.errorMsg = ''; 
     file.upload = Upload.upload(documentsFactory.uploadDocument(
      $scope.id_location, 
      $scope.documentName, 
      $scope.documentDescription, 
      file, 
      $scope.locationUniqueId 
     )); 
     file.upload.then(function (response) { 
      $scope.documentName = $scope.documentDescription = $scope.userFile = ''; 
      documentsFactory.getDocuments($scope.id_location).then(function (data) { 
       $scope.documents = data; 
      }); 
      $timeout(function() { 
       file.result = response.data; 
      }); 
     }, function (response) { 
      if (response.status > 0) 
       $scope.errorMsg = response.status + ': ' + response.data; 
     }, function (evt) { 
      // Math.min is to fix IE which reports 200% sometimes 
      file.progress = Math.min(100, parseInt(100.0 * evt.loaded/evt.total)); 
     }); 
    }else{ 
     $scope.errorMsg = 'Name should be at least 5 chars long'; 
    } 
}; 

这是我厂

factory.uploadDocument = function(id_location, name, description, file, locationUniqueId){ 
     return $http({ 
      method: 'POST', 
      url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents', 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
      data: { 
       id_location: id_location, 
       name: name, 
       description: description, 
       userFile: file, 
       locationUniqueId: locationUniqueId 
      } 
     }).then(function successCallback(response){ 
      return response.data; 
     },function errorCallback(response){ 
      console.log('Error uploading documents: ' + response); 
     }); 
    }; 

UPDATE: ,如果我做“上传请求”在我的控制器工作示例

file.upload = Upload.upload({ 
    url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents/', 
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
    data: { 
     id_location: $scope.id_location, 
     name: $scope.documentName, 
     description: $scope.documentDescription, 
     userFile: file, 
     locationUniqueId: $scope.locationUniqueId 
    } 
}); 

如果您需要任何其他炎症,请让我知道,我会提供。谢谢你在前进

+0

如果您在$ http调用之前记录“$ location.protocol()+'://'+ $ location.host()+'/ rest/api/document/documents'”,输出是什么? – tpsilva

+0

没关系(http://myapp.com/rest/api/document/documents/) –

回答

1

继错误堆栈:

ng-file-upload repository.

this.upload = function (config, internal) { 

这是由你叫有

Upload.upload(documentsFactory.uploadDocument(
     $scope.id_location, 
     $scope.documentName, 
     $scope.documentDescription, 
     file, 
     $scope.locationUniqueId 
    )); 

线330

return sendHttp(config); 

李NE 144

uploadWithAngular(); 

线91

$http(config).then(function (r) { 

当错误被抛出。它看起来像Upload.upload不接受承诺,但为$ http调用的配置。


编辑

什么返回的配置对象?

factory.uploadDocument = function(id_location, name, description, file, locationUniqueId){ 
    return { 
     method: 'POST', 
     url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents', 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     data: { 
      id_location: id_location, 
      name: name, 
      description: description, 
      userFile: file, 
      locationUniqueId: locationUniqueId 
     } 
    } 
}; 

最好的办法是将上传到工厂并返回承诺。

factory.uploadDocument = function(id_location, name, description, file, locationUniqueId){ 
    return Upload.upload({ 
     method: 'POST', 
     url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents', 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     data: { 
      id_location: id_location, 
      name: name, 
      description: description, 
      userFile: file, 
      locationUniqueId: locationUniqueId 
     } 
    }); 
+0

hm,thx对于这个,但是请你也请举个例子,我应该如何准备调用Upload.upload的数据({} )功能还是不可能通过工厂来做到这一点? –

+0

我已经添加了工作代码,它看起来像我的控制器功能上传,如果我在控制器中发出请求 –

+0

我也编辑了我的答案。看一看。只从服务中返回配置对象可能会解决问题。 – tpsilva

相关问题