I have a rails 4 application where I have a contact form which I post to my rails endpoint to process it, send out an email, and then send back json with a redirect url to the client.
my html form is like so:
<form name="contact_form" action="/contact" class="forms" >
<fieldset>
<ol>
<li class="form-row text-input-row name-field">
<input type="text" name="name" class="text-input defaultText required" title="Name (Required)"/>
</li>
<li class="form-row text-input-row email-field">
<input type="text" name="email" class="text-input defaultText required email" title="Email (Required)"/>
</li>
<li class="form-row text-input-row subject-field">
<input type="text" name="subject" class="text-input defaultText" title="Subject"/>
</li>
<li class="form-row text-area-row">
<textarea name="message" class="text-area required"></textarea>
</li>
<li class="form-row hidden-row">
<input type="hidden" name="hidden" value="" />
</li>
<li class="button-row">
<input onclick="contact_email()" type="submit" value="Submit" name="submit" class="btn btn-submit bm0" />
</li>
</ol>
<input type="hidden" name="v_error" id="v-error" value="Required" />
<input type="hidden" name="v_email" id="v-email" value="Enter a valid email" />
</fieldset>
</form>
My javascript function is:
function contact_email(){
form = $('form')
form_data = document.forms.contact_form;
$.ajax({
type: "POST",
url: form.attr('action'),
data: form_data,
dataType: "JSON",
async: false,
success: function(data) {
alert(data);
window.location = data.redirect_url;
},
error: function(xhr, status) {
alert(status);
}
});
}
(I have tried with an without async: false many times, as well as return false; at the end of function without it working)
and my rails endpoint to process the POST
def contact_email
UserMailer.contact_email(params).deliver
flash[:message] = "Thank you for contacting us! We'll be in touch shortly"
res = {:redirect_url => root_url}
render :json => JSON.generate(res)
end
After my javascript fires, the contact_email endpoint is hit, and it sends out the JSON response perfectly fine.
The response is
{"redirect_url":"http://localhost:3000/"}
Yet neither my ajax success or error callback is ever executed.
Why is this?
EDIT:
This may not be relevant, but I do see one console error in browser dev tools
Uncaught TypeError: Illegal invocation
for jquery-1.11.1.js
data: form_data,you putdata: form.serialize(),does the Illegal Invocation error go away, and does anything else happen?contact_email()function fired ?type="submit", then you need to do areturn falseat the end of the function to prevent it from submitting.