2017-03-16 29 views
0

我有一個JavaScript代碼當按下一個鏈接,使一個交請求,的JavaScript調用錯誤彈簧柱方法

$(document).on("click", "#name", function() { 
    try{ 

    var userid= $(this).data('id'); 


    var form = document.createElement('form'); 
     form.setAttribute('method', 'post'); 
     form.setAttribute('action', '/biz/getadmindsr'); 

     var hiddenField = document.createElement("input"); 
       hiddenField.setAttribute("type", "hidden"); 
       hiddenField.setAttribute("name", "userid"); 
       hiddenField.setAttribute("value", userid); 

       form.appendChild(hiddenField); 


     document.body.appendChild(form); 

     form.submit(); 
    } 
    catch(err) 
    { 

     alert('Inside excpetion'+err.message); 
    } 
}); 

然而正在調用的彈簧控制器被如下,

@RequestMapping(value="/get_admindsr_employeelist", method = RequestMethod.GET) 
    public String getdsr_page(ModelMap model) 

期望的控制器如下,

@RequestMapping(value="/getadmindsr", method = RequestMethod.POST) 
    public String getdsr(ModelMap model,@RequestParam("userid") String userid) 

當我通過郵遞員請求第二個控制器r被正確調用,但是當通過javascript代碼段調用時,它會調用第一個控制器。

回答

0

一個潛在的原因可能是在創建並提交表單之前,鏈接被點擊。

你應該避免通過取代:

$(document).on("click", "#name", function() {

有:

$(document).on("click", "#name", function (event) { event.preventDefault(); // ...

+0

謝謝你,現在運轉。問題與你所說的一樣。 – AJN