2016-08-16 86 views
1

如何在控制器類中獲取公司名稱,當我打印字符串cname = req.getParameter(「companyName」)時,它顯示未定義。如何從JS函數獲取數據到控制器類

添加快照以供參考。

JS

編輯公司控制器

@RequestMapping(value = "/editCompany", method = RequestMethod.GET) 
    public ModelAndView editCompany(ModelAndView model, HttpServletRequest req, HttpServletResponse res) { 

     String cname = req.getParameter("companyName"); 
     System.out.println(cname); 

     //int companyId = companyDAO.GetCompanyId(req.getParameter("companyName")); 
     Company company = companyDAO.getInfo(req.getParameter("companyName")) ; 
     model.addObject("company", company); 
     model = getDropdownList(model); 
     model.setViewName("editCompany"); 
     return model; 
    } 

JS

$("#EditBtn").click(function(e) { 
     var companyName=$("#table tr.trActive td:first").html(); 
     $ 
    .ajax({ 
     type : "GET", 
     url : "editCompany?companyName="+companyName, 
     data : JSON.stringify(companyName), 
     contentType: "text/plain", 
     success : function(data) { 

     alert('sucess'); 
     //viewCompanyViaAjax(); 
     }, 
     error : function() { 
     alert('Error occured'); 
     } 



}); 

Updated snapshot

回答

0

In JS 您不需要兩種變體的數據,因爲它是Get,而不是Post方法。

第一變體: 在JS

url : "editCompany/"+companyName 

編輯公司控制器

@RequestMapping(value = "/editCompany/{companyName}", method = RequestMethod.GET) 
    public ModelAndView editCompany(@PathVariable String companyName, ModelAndView model, HttpServletResponse res) { 
System.out.println(companyName); 
... 

第二變形 在JS

url : "editCompany?companyName="+companyName 

編輯公司控制器

@RequestMapping(value = "/editCompany", method = RequestMethod.GET) 
    public ModelAndView editCompany(@RequestParam("companyName") String companyName, ModelAndView model, HttpServletResponse res) { 
System.out.println(companyName); 
... 
相關問題