Q:

Ajax redirect to another page on success

I want to make registration new user with HTTP POST method using Ajax and Spring as backend. 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). The problem is that, I can't redirect after processing to another page eg. 'registerSuccess.jsp'. All the time it's redirecting me to the 'register' page, appending all form values as 'GET' params. 
Ajax function:
       $('.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:
@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.

A:

It really sounds like the default submit action is still happening, you can try one of the following:
use e.preventDefault() as already suggested:
$('.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:
$('.ui.form').on('submit', function(){
        $.ajax({
          ...
          success : function(data){
              ...
          },
          error : function(xhr, status){
              ...
          }
        })
    })
    return false;
});

