I’m trying to get a simple true/false json return from a JsonAction, but it keeps returning as html, not firing my $(post) callback function.
Here are the codes:
[HttpPost]
public JsonResult RegisterPartial(RegisterModel model)
{
return Json(new { Success = false }, JsonRequestBehavior.AllowGet);
}
JQuery in .cshtml
$(function () {
$('#RegisterPartial').live("submit", (function (e) {
e.preventDefault();
$.post($(this).attr("action"), $(this).serialize(), function (retorno) {
if (retorno.Success) {
alert('success');
window.location(gup("returnUrl"));
}
}, "json");
}))
});
here’s response in browser (validated in jasonlint.com):
{"Success":false}
I also tried contentTypes “application/json” and Encoding.UTF8 but response is always an html page instead of firing the jquery callback function. Also tried $.ajax() but still same response.
Thanks a lot for any help!
It is not clear what
#RegisterPartialis. Is it a form? It should be as you are subscribing to the submit event. So this form should look like this:Is this the case? Also you seem to have a javascript error (you have a trailing
)) when closing the.live()anonymous function: