简体   繁体   English

Ajax成功重定向到另一个页面

[英]Ajax redirect to another page on success

I want to make registration new user with HTTP POST method using Ajax and Spring as backend. 我想使用Ajax和Spring作为后端使用HTTP POST方法注册新用户。 Then I write a function to send JSON to controller and it's working well (tested, I can receive form values and persist data in DB). 然后,我编写了一个将JSON发送到控制器的函数,并且它运行良好(经过测试,我可以接收表单值并将数据持久保存在DB中)。 The problem is that, I can't redirect after processing to another page eg. 问题是,例如处理后,我无法重定向到另一个页面。 'registerSuccess.jsp'. 'registerSuccess.jsp'。 All the time it's redirecting me to the 'register' page, appending all form values as 'GET' params. 一直以来,它一直将我重定向到“注册”页面,并将所有表单值都附加为“ GET”参数。

Ajax function: Ajax功能:

       $('.ui.form').on('submit', function(){
            $.ajax({
                url: '/PhotoAlbum/user/register/',
                type: "post",
                data: formToJSON(),
                dataType : "json",
                contentType: 'application/json; charset=utf-8',
                async: false,
                success : function(data){
                    console.log("Inside Success");
                    console.log(data);
                    window.location.href = "/PhotoAlbum/user/regsuccess.jsp";
                },
                error : function(xhr, status){
                    console.log(status);
                }
            })
        })
    })
;
      function formToJSON() {
          return JSON.stringify({
              "firstName": $('.ui.form').form('get value', 'firstName'),
              "lastName": $('.ui.form').form('get value', 'lastName'),
              "email": $('.ui.form').form('get value', 'email'),
              "password": $('.ui.form').form('get value', 'password'),
              "matchingPassword": $('.ui.form').form('get value', 'matchingPassword')
          })
      }

@Controller method: @Controller方法:

@RequestMapping(value = "register/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
        consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody GenericResponse registerUserAccount(@RequestBody UserDTO user, HttpServletRequest request)
{
    LOGGER.debug("Registration of account: ", user);

    User user1 = new User();

    user1.setFirstName(user.getFirstName());
    user1.setLastName(user.getLastName());
    user1.setEmail(user.getEmail());
    user1.setPassword(user.getPassword());

    userRepository.save(user1);    

    return new GenericResponse("success");
}

Console logs: 控制台日志:

Inside Success
(index):134 
Object {message: "success", error: null}

Navigated to http://localhost:8080/PhotoAlbum/user/register/?firstName=John&lastName=…email=john%40gmail.com&password=SecretPass&matchingPassword=SecretPass

Thank's for help. 感谢帮助。

I suggest you to use: 我建议您使用:

location.replace("/PhotoAlbum/user/regsuccess.jsp");

That way, you're restricting the user to use back button. 这样,您将限制用户使用后退按钮。

It really sounds like the default submit action is still happening, you can try one of the following: 听起来真是默认提交行动仍在进行之中,你可以尝试以下之一:

use e.preventDefault() as already suggested: 如已建议使用e.preventDefault()

$('.ui.form').on('submit', function(e){ 
   e.preventDefault();
...

Or, since submit is a jQuery handler, you can try return false which will prevent the event from bubbling as well: 或者,由于submit是一个jQuery处理程序,因此您可以尝试return false ,这也将防止事件冒泡:

$('.ui.form').on('submit', function(){
        $.ajax({
          ...
          success : function(data){
              ...
          },
          error : function(xhr, status){
              ...
          }
        })
    })
    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM