【问题标题】:Am I incorrectly mapping an Ajax request to my Controller Class?我是否错误地将 Ajax 请求映射到我的控制器类?
【发布时间】:2018-03-15 12:47:00
【问题描述】:

我在 JSP 页面上从用户那里获取纬度和经度值,并将其传递给 Controller 类,该类包含一个方法,该方法将根据纬度和经度查询数据库并返回有关房价的信息。我想在新的 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'
     });

    }

Controller类中的方法

    @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,但打印语句仍然打印正确的查询结果。

根据研究,我发现添加 @ResponseBody 可能会解决问题,但是当我像这样向控制器添加 @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)

【问题讨论】:

  • 你没有通过HousePrice
  • 您介意详细说明一下吗?
  • parseHousePrice 控制器中的方法接受三个参数,纬度、经度和一个 HousePrice 类型的对象,您传递的是 lat 和 long,但没有传递 HousingPrice 对象

标签: javascript java ajax jsp spring-boot


【解决方案1】:

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

当 AJAX 只传递纬度和经度时,意味着控制器需要来自传入 HTTP 请求的 3 个值。

【讨论】:

  • 嘿@gargkshitiz 感谢您的回复。我删除了housePriceObject,因为我实际上并不认为我需要它。但是我仍然收到There was an unexpected error (type=Bad Request, status=400). Required double parameter 'latitude' is not present 的错误。这个问题还有其他原因吗?
  • 在浏览器中调试 parseHousePrice 并查看它传递的值
  • 浏览器调试只显示ajax查询发送给控制器的经纬度,控制器调试显示根据经纬度查询的结果。
  • 如果在控制器类中调试日志后看到查询结果,说明请求已经进来了。那为什么会在日志中看到400错误呢?你能从任何 REST 客户端检查你的 API,比如 Fiddler 吗?
猜你喜欢
  • 1970-01-01
  • 2010-12-07
  • 2015-02-18
  • 2014-08-19
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多