簡體   English   中英

Spring MVC中的Ajax發布請求

[英]Ajax post request in Spring MVC

我目前正在嘗試從JavaScript onchange處理程序調用ajax請求以調用Spring MVC控制器。 我認為我當前在視圖中調用URL的方式是錯誤的,因為觸發事件並調用url時,我在瀏覽器上收到404錯誤 如果我一切設置正確,誰能發光呢? 這是我的代碼:

@Controller
public class DataTableController 
{     
    @RequestMapping(value = "/table", method = RequestMethod.GET)
    public String home(Model model) throws JsonGenerationException, JsonMappingException, IOException 
    {
        List<String> gpnList = new ArrayList<GPN>();
        gpnList.add("GPN-1"); gpnList.add("GPN-2"); gpnList.add("GPN-3"); 
        model.addAttribute("gpnList", mapper.writeValueAsString(gpnList)); 
        return "index"; //name of my view
    }

    @RequestMapping(value = "/getsector", method = RequestMethod.POST)
    public @ResponseBody String getSector(@RequestParam("market")String market) throws JsonGenerationException, JsonMappingException, IOException
    {
        List<String> sectors = new ArrayList<String>();
        sectors.add("Auto"); sectors.add("Industrial"); sectors.add("Analog");
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(sectors);
    }
}

jQuery代碼:

$(document).ready(function()
{
    document.getElementById("markets").onchange =  function()
    { 
        var market = $("select[id='markets'").find('option:selected').text();
        var filters = { "market" : market }
        filters = JSON.stringify(filters);

        $.ajax({
            url: "/getsector",
            type: "POST",
            dataType : 'json',
            contentType : "application/json",
            data: JSON.stringify(filters),
            success: function(response) 
            {
                console.log("sucess!");
            },
            error: function(e){
                console.log("ERROR: ", e);
            }
        });
    }
}); 

我要實現的主要目的是能夠通過ajax調用來調用我的控制器。 有關Spring控制器映射和約定的任何其他技巧將不勝感激。 提前致謝!

如果您是請求信息,則應使用GET請求,而不是POST

您正在將@RequestParam與json負載混合。 如果您想將過濾器作為請求參數接收,則必須使用類似以下內容的網址,而不是json負載:

$(document).ready(function()
{
    document.getElementById("markets").onchange =  function()
    { 
        var market = $("select[id='markets'").find('option:selected').text();

        $.ajax({
            url: "/getsector?market="+market,
            type: "GET",
            success: function(response) 
            {
                console.log("sucess!");
            },
            error: function(e){
                console.log("ERROR: ", e);
            }
        });
    }
}); 

@RequestMapping(value = "/getsector", method = RequestMethod.GET)
public @ResponseBody String getSector(@RequestParam("market")String market) throws JsonGenerationException, JsonMappingException, IOException
{
  .... your logic.
}

另一方面,如果您真的想對JSON有效負載使用POST請求,則需要在控制器上使用@RequestBody並將json對象綁定到具有相同屬性的bean。

@RequestMapping(value = "/getsector", method = RequestMethod.POST)
public @ResponseBody String getSector(@RequestBody Market market) throws JsonGenerationException, JsonMappingException, IOException
{
    List<String> sectors = new ArrayList<String>();
    sectors.add("Auto"); sectors.add("Industrial"); sectors.add("Analog");
    return sectors;
}


public class Market
{
  String market;

  //getter and setter...
}

請記住,您的JavaScript也是錯誤的,您使用JSON.stringify兩次。

如果您使用@ResponseBody並且spring配置良好,它將在返回響應時為您進行序列化,因此您不必手動進行。

這段代碼尚未經過測試,僅供您參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM