2017-02-01 59 views
0

您好我正在开发一个Web应用程序与api的API。我正在做文件上传模块。文件上传完成后,我在返回对象时遇到问题。如何使用angularjs从web api控制器返回数据?

下面是我的API代码来保存文件相关的数据到数据库,如果它是succsfull我返回对象。

NCT_FileUpload obj = new NCT_FileUpload(); 
obj.file_path = uploadPath; 
obj.user_id =9; 

entityObject.NCT_FileUpload.Add(obj); 
int result = entityObject.SaveChanges(); 
if (result == 1) 
{ 
    return Request.CreateResponse<NCT_FileUpload>(HttpStatusCode.OK, obj); 
} 
else 
{ 
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "1"); 
} 

这是我的angularjs代码。

$scope.uploadFiles = function() { 
    $scope.uploading = true; 
    uploadService.uploadFiles($scope) 
     // then() called when uploadFiles gets back 
     .then(function (data) { 
      // promise fulfilled 
      $scope.uploading = false; 
      if (data === '') { 
       alert("Done!!!") 
       $scope.formdata = new FormData(); 
       $scope.data = []; 
       $scope.countFiles = ''; 
       $scope.$apply; 
      } else { 
       alert("Shit, What happended up there!!! " + data); 
      } 
     }, function (error) { 
      $scope.uploading = false; 
      //Server Error 
      alert("Shit2, What happended up there!!! " + error); 
     } 
    ); 
}; 

下面是angularjs

if (typeof response.data === 'string') { 
    return response.data; 
} else { 
    return $q.reject(response.data); 
} 

在这里,我想检查与对象,而不是作为字符串我的服务代码。

我能够保存数据在服务器,如果我把下面的代码在API控制器我能够显示完成。但是我正在返回对象,所以我的数据不会为空。目前我的错误功能正在执行。我想在成功函数中处理从api返回的对象。有没有办法做到这一点?任何帮助,将不胜感激。谢谢。

return new HttpResponseMessage(HttpStatusCode.OK) ; 

回答

0

我认为这里的问题是通用参数。更改此:

return Request.CreateResponse<NCT_FileUpload>(HttpStatusCode.OK, obj); 

要这样:

return Request.CreateResponse(HttpStatusCode.OK, obj); 
相关问题