2013-03-04 110 views
0

在我的jQuery的功能,我使用重定向像window.location.href这样:如何在jQuery的重定向使用POST而不是GET

window.location.href = "${pageContext.request.contextPath}/redirectUser.ajax?login="+response.result.name; 

它工作正常,但我看到这个字符串在浏览器这样的:

http://localhost:8080/task7/redirectUser.ajax?login=user 

我控制器还使用GET

@RequestMapping (value="/redirectUser.ajax",method = RequestMethod.GET) 
    public String forwardUserToUsersPage(ModelMap model, HttpServletRequest req){ 
     User foundedUser = userDao.findByLogin(req.getParameter("login")); 
     req.getSession().setAttribute("user", foundedUser); 
     return "userPage";//to WEB-INF/jsp/userPage.jsp 
    } 

我怎么可以重写应用程序,这部分才能使用r POST方法重定向并在控制器中处理?

下面是从servler收到回复功能

function doAjaxPost() { 
    // get the form values 
    var queryString = $('#loginform').formSerialize(); 

    $.ajax({ 
    type: "POST", 
    url: "${pageContext. request. contextPath}/loginUser.ajax", 
    data: queryString, 
     //"name=" + name + "&pswd=" + pswd, 

    success: function(response){  
     // we have the response 
     var delay = 1500; 
     if (response.status == "OK_USER") { 
      $('#error').html(''); 
      $('#info').html("Login exists, password is correct everything will be fine.<br> Redirect to User's page"); 

      //var delay = 3000; 
      setTimeout(function() { 
      window.location.href = "${pageContext.request.contextPath}/redirectUser.ajax?login="+response.result.name; 
      }, delay); 

     } 
.... 

所以,我怎么能重定向使用jQuery或别的东西使用POST方法(功能实际上一部分)?

回答

1

的最快方法我看到:

创建<form>节点,设置它的action正确的URL和method="post",与所需的参数(在<input>字段)填充它,并调用.submit()

例如:

... 
setTimeout(function(){ 
    var $form = $('<form>').attr({ 
     action: "${pageContext.request.contextPath}/redirectUser.ajax", 
     method: "post" 
    }); 

    $form.append('<input name="login" value="'+response.result.name'" />'); 
    $form.submit(); 
}, delay); 
... 
+0

有一种形式,使用jQuery验证插件进行验证。该函数从控制器获取答案,然后将用户重定向到userPage.jsp或adminPage.jsp。 我通过使用简单的JavaScript做到了这一点window.location.href 也许在jQuery中有相同的功能? – 2013-03-04 08:50:54

+0

不幸的是,你的解决方案不起作用。 我只是在我的浏览器页身中得到这个字符串: {“status”:“OK_USER”,“result”:{“name”:“user”,“pswd”:“123”}} I've将控制器更改为POST并添加@ModelAttribute(value =“login”)字符串登录, 仍然不起作用 – 2013-03-04 09:25:42

+0

在这种情况下,您没有给出正确的URL来登录用户。如果指定'redirectUser.ajax'通过'ajax'进行灌溉并返回以'json'格式化的数据,你应该这样使用它。看看你的应用程序的逻辑,并检查用户应该如何进行身份验证。 – LeGEC 2013-03-04 09:48:46

相关问题