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

问题描述 投票:0回答:1

我试图获取存储在数据库中的特定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;
   }

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

angularjs post web-applications get restful-url
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.