I have an AJAX form that I'm looking to submit the data via AJAX to a function for a controller in the background. Right now, I have the JS below and am using the controller code below to test. However, while the function returns a 200 response and I can see the response in Firebug the success and error functions are not running. Any ideas?
$('contact_form').submit(function() {
var valuesToSubmit = $(this).serialize();
$.ajax({
url: $(this).attr('action'),
data: valuesToSubmit,
dataType: 'json',
type: 'POST',
success: function(data) {
alert('success');
},
error: function(data) {
alert('error');
}
});
return false; // prevents normal behavior
});
I've tried code beyond alerts and it still doesn't work. Also, here's the controller below. Help is greatly appreciated
def ajax_response
render :json => {
:name => 'test'
}
end
*Edit - Adding HTML form in case that's relevant here
<form accept-charset="UTF-8" action="/contact_message" data-remote="true" id="contact_form" method="post">
<label for="name">Name</label><span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input class="form-control" id="name" name="name" type="text" />
</div>
</div>
<label for="email">Email</label><span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input class="form-control" id="email" name="email" type="text" />
</div>
</div>
<label for="message">Message</label><span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-11 col-md-offset-0">
<textarea class="form-control" id="message" name="message" rows="8"></textarea>
</div>
</div>
<p>
<button class="btn-u" name="button" type="submit">Send Message</button>
</p>
</form>