Simple JQuery Form Submission with ASP.NET MVC 3
I have a very basic email submission form in my ASP.NET MVC 3 app. I'm trying to integrate the jQuery form plug-in with it.
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.form.js")"></script>
<script type="text/javascript">
// prepare the form when the DOM is ready
$(document).ready(function () {
var options = {
beforeSubmit: showRequest,
success: showResponse
};
// bind form using 'a开发者_JAVA技巧jaxForm'
$('#mailform').ajaxForm(options);
});
function showRequest(formData, jqForm, options) {
alert('request');
}
// post-submit callback
function showResponse(responseText, statusText, xhr, $form) {
alert('response');
}
</script>
...
<form action="@Url.Action("SubmitEmail")" class="mail-form" id="mailform" method="post">
<fieldset>
<span class="txt">
<input type="text" name="email" id="email" value="Enter Your Email Address" />
</span>
<input class="button" id="submitemail" type="submit" />
</fieldset>
</form>
I've tested my non-AJAX version and it works just fine. When I try to use the jQuery plug-in, however, the showRequest
function gets called, but the showResponse
function doesn't-- and it doesn't seem like the method is ever called on the server either.
A couple things to check - you have a script tag for jQuery form but I don't see one for jQuery itself (you'd want to have jQuery listed before the plugin).
Also - install Fiddler or Firebug and make sure that requests are being sent out. And just to throw this out there, for a call this simple the form plugin is overkill. $.post would work just fine.
精彩评论