I am working on a view containing two forms, handled by separate controller actions which return a string serilaized to Json:
return Json(message);
The forms are submitted using jQuery, by clicking on a button outside the two forms. The button handler:
$('#inviteForm').ajaxSubmit({
success: function(html, status) {
$("#response").text(html);
}
})
$('#trialForm').ajaxSubmit({
success: function(html, status) {
$("#response").append(html);
}
});
The browser receives the result and prompts the user to download as it is interpreted as "application/json".
However, if I only submit one of these forms in the jQuery, the resulting Json message is displayed as a string in the #response element as desired.
Why does adding a second ajaxSubmit() cause this different behaviour?
Thanks.
The view contains the following forms:
<form action="/Controller1/SaveAttachments/<%=Model.ObjectId %>" id="trialForm" method="post" enctype="multipart/form-data">
<input type="file" name="trialForm" size=30/>
<input type="file" name="trialSheet" size=30/>
<input type="file" name="trialApproval" size=30/>
</form>
and...
<form action="/Controller1/UpdateTemplate/<%=Model.ObjectId %>" id="inviteForm" method="post" enctype="multipart/form-data">
<%=Html.TextArea("invitationSheet", Model.InvitationSheet,
new { @name = "invitationSheet"})
<script type="text/javascript">
window.onload = function() {
var sBasePath = '<%=Url.Content("~/Content/FCKeditor/")%>';
var oFCKeditor = new FCKeditor('invitationSheet');
oFCKeditor.BasePath = sBasePath;
oFCKeditor.HtmlEncodeOutput = true;
oFCKeditor.ReplaceTextarea();
}
</script>
</form>
-
Have you tried changing the second submit to setting the text instead of appending to the DOM? That's the glaring difference between the two, but I'm not sure why it would cause that behavior.tvanfosson– tvanfosson2009年09月25日 14:24:19 +00:00Commented Sep 25, 2009 at 14:24
-
Thanks for your suggestion. I've tried both .text() and .append() with no difference in behaviour. A further point is the result of the first ajaxSubmit is displayed in #response - it is the result of the second ajaxSubmit which is interpreted as "application/json".TonE– TonE2009年09月25日 14:27:55 +00:00Commented Sep 25, 2009 at 14:27
-
I've just tried commented out all code from the controller actions, so the code is now return Json("Action1") and return Json("Action2"). Still getting the same behaviour. Will add the code for the forms on the view to my original post...TonE– TonE2009年09月25日 14:35:21 +00:00Commented Sep 25, 2009 at 14:35
-
After thinking about it more, I think the issue is client-side. See my answer for a couple of idea.tvanfosson– tvanfosson2009年09月25日 14:40:56 +00:00Commented Sep 25, 2009 at 14:40
-
The problem is the file inputs. See my updated answer.tvanfosson– tvanfosson2009年09月25日 14:56:40 +00:00Commented Sep 25, 2009 at 14:56
1 Answer 1
Update:
You can't upload files directly via AJAX so it is doing an actual post of the form containing file inputs. You should look at a plugin that will let you upload files using the hidden iframe technique that works asynchronously instead of trying to upload using AJAX.
4 Comments
Explore related questions
See similar questions with these tags.