This is a strange one. I have a Surface controller for an Ajax contact form. I integrated reCaptcha on that form and have an issue. When the reCaptcha is invalid, all logic works and I return a Json value of "Success = false" but when returned to the PartialView, it fires the OnSuccess script and not the OnFailure script. The correct scripts are fired when the form is successful.
Returning a JSON object that has a property you happen to have named success does not affect the actual success or failure of the response.
I imagine the JSON result is passed to the OnSuccess function (not sure of how exactly), but it would be in your ShowContactSuccess method that you'd want to check the JSON result to see if the success property is set to true or false and respond accordingly.
OnFailture and OnSuccess are defined properties of Ajax.BeginForm() (AjaxOptions), you just tell it what function to fire based on the returned result (true or false). I do not know how to access this result.
These are the 2 functions that can be called.
function ShowContactError() {
$("#submit-status").html("<p style='color: red;'>There was an error while processing your request. <a style='color: red;' data-dismiss='modal' aria-hidden='true'>Click Here</a> to close this window</p>")
$("#submit-status").show();
}
function ShowContactSuccess() {
//alert("Showing Success");
$("#submit-status").html("<p style='color: white;'>The request form was successfully transmitted. <a style='color: white;' data-dismiss='modal' aria-hidden='true'>Click Here</a> to close this window</p>")
$("#submit-status").show();
}
function HandleResponse(data) {
if (data.success)
// Fire Success Message
else {
if (data.captureerror)
// Fire Captcha Specific Error
else
// Fire General Error Message
}
}
Ajax form Json success is false but returns true
This is a strange one. I have a Surface controller for an Ajax contact form. I integrated reCaptcha on that form and have an issue. When the reCaptcha is invalid, all logic works and I return a Json value of "Success = false" but when returned to the PartialView, it fires the OnSuccess script and not the OnFailure script. The correct scripts are fired when the form is successful.
Here is the controller code:
This is in the header of the PartialView:
{
Can anyone see an obvious mistake? I have 2 forms that are doing this.
Thanks
Phillip
Returning a JSON object that has a property you happen to have named
success
does not affect the actual success or failure of the response.I imagine the JSON result is passed to the
OnSuccess
function (not sure of how exactly), but it would be in yourShowContactSuccess
method that you'd want to check the JSON result to see if thesuccess
property is set to true or false and respond accordingly.OnFailture and OnSuccess are defined properties of Ajax.BeginForm() (AjaxOptions), you just tell it what function to fire based on the returned result (true or false). I do not know how to access this result.
These are the 2 functions that can be called.
https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions(v=vs.118).aspx
This shows how you can use the data in
OnSuccess
: http://stackoverflow.com/questions/8034530/asp-net-ajax-beginform-onsuccess-call-back-with-paramsThat was a good find. Many thanks. I was able to enhance my error responses and is now working like a charm
client script
is working on a reply...