I am a bit mystified by how to utilize templates for Guest entries plugin working fine for normal post saving custom fields.
When I use ajax to submit I get an XHR 200 response but an HTML response - no JSON. Ideally I don't want to redirect to another page but just manipulate the DOM and pass some variables into the page form JSON returned on success.
I have read and reread every ajax post but still can't get a pure JSON response
Is it necessary to utilize some kind of detection within the template for Craft ajax?
Just when I thought i had ajax understood...
Here is my JS:
$(document).ready(function () {
//validation rules
$("form#brick").validate({
ignore: ":hidden",
rules: {
"fields[firstName]": {
required: true,
},
"fields[lastName]": {
required: true
},
"fields[email]": {
required: true,
email: true
}
},
messages: {
"fields[email]": "Email is required"
},
submitHandler: function (form) {
// Prevent default posting of form
event.preventDefault();
var data = $(this).serialize();
$.ajax('/', data, function(response) {
if (response.success) {
console.log(response);
$('#thanks').fadeIn();
$('#brick').fadeOut();
console.log(response);
} else {
// response.error will be an object containing any validation errors that occurred, indexed by field name
// e.g. response.error.fromName => ['From Name is required']
alert('An error occurred. Please try again.');
}
});
}
});
});
function (form) {...
should befunction (event) {...
; the bug means you're not properly preventing the form's default submission. Not sure if that's the root cause of your issue, though.