Ajax front-end user registration form - not returning JSON I have a user registration form on the front-end of a site that is working well as a standard form, but when I try to use ajax submission, I'm getting an HTML response rather than JSON. 

Here's the javascript I'm using

$('#register-form').submit(function(ev) {
    ev.preventDefault();

    var data = $(this).serialize();

    $.post('/', data, function(response) {

        console.log(response);

        if (response.success) {
            alert("success")
        } else {
            alert(response.error);
        }
    });
});


And this is my form code:

&lt;form method="post" action="" id="register-form" accept-charset="UTF-8"&gt;
{{ getCsrfInput() }}
&lt;input type="hidden" name="action" value="users/saveUser"&gt;

&lt;h3&gt;Create An Account&lt;/h3&gt;
&lt;p&gt;By creating an account, you will be able to save your application progress. This is an optional step.&lt;/p&gt;

&lt;div class="row"&gt;
    &lt;label for="email" class="col-sm-3 control-label"&gt;Email&lt;/label&gt;
    &lt;input type="text" class="form-control" id="email" name="email" {%- if account is defined %} value="{{ account.email }}"{% endif %}&gt;
&lt;/div&gt;

&lt;div class="row"&gt;
    &lt;div class="form-group col-6"&gt;
        &lt;label for="password"&gt;Password&lt;/label&gt;
        &lt;input id="password" class="form-control" type="password" name="password"&gt;
    &lt;/div&gt;

    &lt;div class="form-group col-6"&gt;
        &lt;label for="password2"&gt;Confirm Password&lt;/label&gt;
        &lt;input id="password2" class="form-control" type="password" name="password2"&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div class="actions"&gt;
    &lt;p&gt;&lt;input type="submit" id="form-submit" value="Register" class="btn btn-grey"/&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;span class="center"&gt;Already have an account? &lt;a href="#" id="sign-in-form-btn"&gt;Sign In&lt;/a&gt;&lt;/span&gt;




The response var is returning the entire html page rather than JSON data.

Is there a way to trigger a JSON response for the registration form?

Note, I'm able to use similar code for a front-end login form and it's working well.
EDIT

Craft 2.4 is now available, and the users/saveUser action will now return a JSON response for AJAX requests.

The original answer is still valid for &lt;2.4 builds:

No, there's no way to trigger a JSON response for the users/saveUser action.

Other UsersController actions (such as users/login do return JSON for AJAX requests, so it might be an idea to submit this as a feature request – as a consistency measure, if nothing else.

For now, here's a couple of ideas for workarounds:


Write a plugin with a controller that wraps the UsersController-&gt;actionSaveUser() method somehow, or implements its own action (you could basically just copy paste the whole actionSaveUser() from the core UsersController, changing it to return some JSON at the end).
You can set the redirect parameter (using a hidden input field in your form) to point to a template where you build the JSON yourself.


Form:

&lt;form method="post" action="" id="register-form" accept-charset="UTF-8"&gt;
     &lt;!-- Your form code --&gt;
     &lt;input type="hidden" name="redirect" value="{{ url( 'path/to/your/template/or/a/route' }}"&gt;
&lt;/form&gt;


Template for the redirect (notice the JSON header):

{% header "Content-Type: application/json" %}
{
    "success" : {{ account is defined and account.id ? true : false }}
}