AngularJS和Spring MVC的Ajax GET错误

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

我有一个角度函数,它使用带有GET和params的$ http请求:

    $scope.getMatchingRecipe = function(){
      $http({
            method: "GET",
            url: "/recipemanagement/getMatchingRecipeList",
            params: {
                matchingText : $scope.matchingRecipe.text
            }
        }).success(function (data) { 
            console.log(data);
            $scope.recipeList = data;
            console.log($scope.recipeList[0]);
        })
        .error(function (data) { 
            console.log(data);
        });  
  };

和MVC控制器一样:

@RequestMapping(value="/getMatchingRecipeList")
    public @ResponseBody String getRecipeDropdownList(@RequestParam(value="matchingText") String matchingText){

        List<Recipe> recipeList = recipeServiceImpl.getMatchingRecipes(matchingText);
        for(Recipe recipe : recipeList){
            System.out.println("recipe :"+recipe.getName());
        }
        List<RecipePO> recipePOList = new ArrayList<RecipePO>();
        System.out.println("List Size :"+recipeList.size());
        for(Recipe recipe : recipeList){
            RecipePO recipePO= new RecipePO();
            recipePO.setId(recipe.getId());
            recipePO.setName(recipe.getName());
            recipePO.setDifficulty(recipe.getDifficulty());
            recipePO.setServes(recipe.getServes());
            recipePOList.add(recipePO);
        }

        try {
            return new ObjectMapper().writeValueAsString(recipePOList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "Error";
        }
    }

但是,当调用函数getMatchingRecipeList时,它返回404.但是当我检查后端控制台(即控制器函数getRecipeDropdownList通过hibernate进行数据库调用,因此它显示在控制台中执行的查询)时,执行该函数。

java angularjs spring-mvc
1个回答
0
投票

似乎问题不在于ajax调用。服务器从后端抛出错误,因此返回error.jsp页面。

问题:服务类是@ Transactional.In DAO层,我使用了session.close()(是的,duh);

分析:我在try-catch块中包装了所有函数,并了解了这个错误。它在stacktrace中抛出了hibernate会话已经关闭的错误。这就是它返回error.jsp页面的地方

解决方案:我从DAO类中删除了session.close()。这解决了这个问题。

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