代码之家  ›  专栏  ›  技术社区  ›  user9269133

我是否错误地将Ajax请求映射到我的控制器类?

  •  0
  • user9269133  · 技术社区  · 7 年前

    我在JSP页面上从用户那里获取纬度和经度值,并将其传递给控制器类,该类包含一个方法,该方法将根据纬度和经度查询数据库并返回房价信息。我想在一个新的JSP页面上打印这些信息,但是,我遇到了多个错误(我将详细介绍这些错误)。控制器成功地运行了查询,因为我有一个打印调试器,可以在Eclipse的控制台中显示正确的信息,但是包含这些信息的新页面JSP页面将不会加载。

    在JSP页面#1上包含AJAX查询的函数

        function parseHousePrice(){
    
           $('.search_latitude').val(marker.getPosition().lat());
           var Lat = marker.getPosition().lat();
           console.log(Lat);
    
           $('.search_longitude').val(marker.getPosition().lng());
           var Long = marker.getPosition().lng();
           console.log(Long);
    
          $.ajax({
            type: "POST",
            url: "/parseHousePrice",
            data: { latitude: Lat, 
                    longitude: Long,  
                  }, 
           datatype: 'json'
         });
    
        }
    

    控制器类中的方法

        @RequestMapping(value = "/parseHousePrice", method={RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody String parseHousePrice(@RequestBody HousePrice housePriceObject, @RequestParam("latitude") double latitude,@RequestParam("longitude") double longitude, Model model) {
    
        double housePriceAverage = parseHousePrice.ParseHousePrice(latitude, longitude);
    
        List<Double> housePriceList = parseHousePrice.getList();
        int housePriceListSize = housePriceList.size();
    
        System.out.println("The average house price for this area is: " + housePriceAverage + " based on " + housePriceListSize + " property prices in this area"); <-- This print statement successfully prints the results fromt he query
    
        model.addAttribute("houseprice", housePriceAverage);
        model.addAttribute("housepricelistsize", housePriceListSize);
    
        return "houseprice"; <-- JSP Page I'm trying to pass the information above to
    }
    

    通过上面设置控制器的方式,我得到了 White Label Error 声称 There was an unexpected error (type=Bad Request, status=400). Required double parameter 'latitude' is not present 但是print语句仍然打印正确的查询结果。

    根据研究,我发现 @ResponseBody 可能会解决问题,但当我添加 @响应主体 这样的控制器

    @RequestMapping(value = "/parseHousePrice", method={RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody String parseHousePrice(@RequestBody HousePrice housePriceObject, @RequestParam("latitude") double latitude,@RequestParam("longitude") double longitude, Model model) {
    
    same code
    
    return "houseprice";
    }
    

    我得到以下错误 There was an unexpected error (type=Bad Request, status=400). Required request body is missing: public java.lang.String com.dit.arearatingsystem.web.UserController.parseHousePrice(com.dit.arearatingsystem.model.HousePrice,double,double,org.springframework.ui.Model)

    1 回复  |  直到 7 年前
        1
  •  1
  •   gargkshitiz    7 年前

    @RequestBody HousePrice housePriceObject, @RequestParam("latitude") double latitude,@RequestParam("longitude") double longitude, Model model

    意味着控制器期望从传入的HTTP请求中得到3个值,而AJAX只传递纬度和经度。