<p><strong>EDIT</strong></p>

<p><strong><a href="http://buildwithcraft.com/updates/2.4" rel="nofollow">Craft 2.4 is now available</a>, and the <a href="https://buildwithcraft.com/classreference/controllers/UsersController#actionSaveUser-detail" rel="nofollow">users/saveUser</a> action will now return a JSON response for AJAX requests.</strong></p>

<p><em>The original answer is still valid for &lt;2.4 builds:</em></p>

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

<p>Other <a href="http://buildwithcraft.com/classreference/controllers/UsersController" rel="nofollow">UsersController</a> actions (such as <code>users/login</code> do return JSON for AJAX requests, so it might be an idea to <a href="http://feedback.buildwithcraft.com/forums/285221-feature-requests" rel="nofollow">submit this as a feature request</a> – as a consistency measure, if nothing else.</p>

<p>For now, here's a couple of ideas for workarounds:</p>

<ol>
<li><p>Write a plugin with a controller that wraps the <code>UsersController-&gt;actionSaveUser()</code> method somehow, or implements its own action (you could basically just <a href="https://raw.githubusercontent.com/pixelandtonic/Craft-Release/master/app/controllers/UsersController.php" rel="nofollow">copy paste the whole <code>actionSaveUser()</code> from the core <code>UsersController</code></a>, changing it to return some JSON at the end).</p></li>
<li><p>You can set the <code>redirect</code> parameter (using a hidden input field in your form) to point to a template where you build the JSON yourself.</p></li>
</ol>

<p>Form:</p>

<pre><code>&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;
</code></pre>

<p>Template for the redirect (notice the JSON header):</p>

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

CC BY-SA 3.0
2015-03-28T20:51:57.610Z
Thanks for the quick response. I just added the feature request, and will give this a shot in the meantime. Cheers.
Coppa
CC BY-SA 3.0
2015-03-28T20:55:14.747Z
Nice – would be great if you'd give an update if/when you get it working; the above are just ideas and the JSON thing may or may not work as-is. It would be great to have a solid answer for other users having the same issue in the future.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-31T04:08:44.830Z
I tried the redirect route, but I wasn't able to output meaningful error message because actionSaveUser() only calls the redirect on successful user creation. So I have some front-end validation in place now to test basic input errors. I did add a redirect to a JSON page, and since it only redirects on success, I'm just returning "success" from this page by default. Then if I don't see the success var (which I won't on error as it returns the whole index page) then I can assume there was an error. At this point the error is most likely due to a duplicate email address or invalid password.
Coppa
CC BY-SA 3.0
2015-03-28T20:38:56.873Z
2015-06-02T22:34:26.867Z
2015-06-02T22:34:26.867Z
Mats Mikkel Rummelhoff
<p>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. </p>

<p>Here's the javascript I'm using</p>

<pre><code>$('#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);
        }
    });
});
</code></pre>

<p>And this is my form code:</p>

<pre><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;
</code></pre>

<p></p>

<p>The response var is returning the entire html page rather than JSON data.</p>

<p>Is there a way to trigger a JSON response for the registration form?</p>

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

<form><ajax><registration>
Ajax front-end user registration form - not returning JSON
# Ajax front-end user registration form - not returning JSON

(Asked by: username_0 on 2015-03-28)

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:

{{ getCsrfInput() }}


Create An Account
By creating an account, you will be able to save your application progress. This is an optional step.


    Email
    



    
        Password
        
    

    
        Confirm Password
        
    



    


Already have an account? Sign In


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.

**Tags:** form, ajax, registration


---

## 1 answers

### Answer by username_1 on 2015-03-28. Score: 5 | Accepted answer

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 <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->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:

     
     


Template for the redirect (notice the JSON header):
{% header "Content-Type: application/json" %}
{
    "success" : {{ account is defined and account.id ? true : false }}
}

> **Comments:**
> **username_0**: Thanks for the quick response. I just added the feature request, and will give this a shot in the meantime. Cheers.  
> **username_1**: Nice – would be great if you'd give an update if/when you get it working; the above are just ideas and the JSON thing may or may not work as-is. It would be great to have a solid answer for other users having the same issue in the future.  
> **username_0**: I tried the redirect route, but I wasn't able to output meaningful error message because actionSaveUser() only calls the redirect on successful user creation. So I have some front-end validation in place now to test basic input errors. I did add a redirect to a JSON page, and since it only redirects on success, I'm just returning "success" from this page by default. Then if I don't see the success var (which I won't on error as it returns the whole index page) then I can assume there was an error. At this point the error is most likely due to a duplicate email address or invalid password.  

---

