无法使用restful api从数据库中获取照片

时间:2018-04-16 05:53:37

标签: angularjs post web-applications get restful-url

我试图获取存储在数据库中的特定userId的照片, 我能够从API生成响应,如下所示: enter image description here  但是它在控制器的getPhotopath()方法的错误部分中被接收,如下所示: enter image description here HTML:

<div class="col-md-3 " ng-init="modifyphoto()" >
            <img class="smaller" ng-src="fetchedimg" ng-model="fetchedimg"><br>
          <p>{{userName}}</p>
          <div ng-model="userType">
                 <p>{{userType | UserFilter}}</p>
          </div>
          <div ng-controller="AskController">
                 <button class="btn btn-default" ng-click="gotouserdetails()">View
                       My Details</button>

          </div>
   </div>

控制器:

$scope.modifyphoto = function() {
          $scope.getPhotoPath();
          if($scope.userPhoto==null){
                 $scope.fetchedimg=$scope.defaultimg;
          }
          else{
                 $scope.fetchedimg=$scope.userPhoto;
          }
   };

   $scope.getPhotoPath = function() {

          var obj = JSON.parse($cookies.get('user'));
          $scope.passUserId = obj.userId;
          $http.get(URI + "user/getphoto"+"/"+$scope.passUserId).then(
                       function(response) {      
                              alert("hifrsegfsfgv");
                              alert(response.data.message);

                              $scope.userPhoto=response.data.userPhoto;
                       }, function(response) {
                              alert("hi");
                              alert(response.data);
                              $scope.userPhoto =null;
                              alert("danger"+response.data.userPhoto);
                       });
   };

API:

@Path("getphoto/{an}")
   @GET
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(MediaType.APPLICATION_JSON)
   public Response getPhoto(@PathParam("an") String userId) throws Exception {
          String returnValue = null;
          Response response = null;
          User user = null;
          try {
                 System.out.println(userId);
                 UserService userService = Factory.createUserService();
                 String photoPath=userService.getPhotoPath(userId);
                 user = new User();
                 user.setUserPhoto(photoPath);
                 System.out.println(photoPath);
                 returnValue = JSONParser.toJson(user);
                 System.out.println(returnValue);
                 response = Response.status(Status.OK).entity(returnValue).build();
          } catch (Exception e) {
                 String errorMessage = AppConfig.PROPERTIES.getProperty(e.getMessage());
                 User user1 = new User();
                 user1.setMessage(errorMessage);
                 String returnString = JSONParser.toJson(user1);
                 response = Response.status(Status.SERVICE_UNAVAILABLE).entity(returnString).build();
          }
          return response;
   }

我不明白后端的状态是否正常,以及为什么在错误部分收到它?

1 个答案:

答案 0 :(得分:0)

尝试以base64格式返回图像以避免JSON Parse错误。并将该图像绑定到img标记