Ok so I have this form in my view:
<form id="MyForm">
<input type="text" name="myinput" />
<button type="submit" />
</form>
I have the following Javascript at the top of my view, which runs when the page loads:
<script type="text/javascript">
$(document).ready(
function () {
$("#MyForm").submit(
function () {
var url = "Home/TestAjax";
var dataToSend = $("#MyForm").serialize();
alert(dataToSend);
$.ajax
(
{
type: "POST",
url: url,
data: dataToSend,
success: function (data) {
alert(data);
}
}
);
return false;
}
);
}
);
</script>
The form is being serialised to ajax correctly, as verified by the alert box. Here is my TestAjax controller method:
[HttpPost]
public string TestAjax(string data)
{
return !string.IsNullOrEmpty(data) ? "Success" : "Failure";
}
The value being returned is Failure, because the AJAX isn't being posted back. What am I doing wrong here?
Thanks