簡體   English   中英

使用 jQuery 在 ASP.NET MVC 中處理 Json 結果

[英]Handling Json results in ASP.NET MVC with jQuery

我正在處理一個包含兩個表單的視圖,由單獨的控制器操作處理,這些操作返回一個序列化為 Json 的字符串:

return Json(message);

通過單擊兩個表單外的按鈕,使用 jQuery 提交表單。 按鈕處理程序:

$('#inviteForm').ajaxSubmit({
  success: function(html, status) {
    $("#response").text(html);
  }
})
$('#trialForm').ajaxSubmit({
  success: function(html, status) {
    $("#response").append(html);
  }
});

瀏覽器接收結果並提示用戶下載,因為它被解釋為“application/json”。

但是,如果我只在 jQuery 中提交這些表單之一,則生成的 Json 消息將根據需要顯示為 #response 元素中的字符串。

為什么添加第二個 ajaxSubmit() 會導致這種不同的行為?

謝謝。

該視圖包含以下形式:

<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>

和...

<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>

更新

您不能通過 AJAX 直接上傳文件,因此它正在對包含文件輸入的表單進行實際發布。 您應該查看一個插件,它可以讓您使用異步工作的隱藏 iframe 技術上傳文件,而不是嘗試使用 AJAX 上傳。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM