【问题标题】:Using jquery to check if POST is successful使用jquery检查POST是否成功
【发布时间】:2013-04-18 18:35:32
【问题描述】:

我正在尝试编写会弹出并说 Post 成功的 jquery 函数!如果没有,则 Post 不成功!我如何在输入一些 jquery 时使用 try catch?

@using (Html.BeginForm("Admin", "Home", "POST"))
{
<div class="well">
    <section>
        <br>
        <span style="font-weight:bold">Text File Destination:&#160;&#160;&#160;</span>
        <input type="text" name="txt_file_dest" value="@ViewBag.GetTextPath">
        <span style="color:black">&#160;&#160;&#160;Example:</span><span style="color:blue"> \\invincible\\chemistry$\\</span>
        <br>
        <br>
        <span style="font-weight:bold">SQL Connection String:</span>
        <input type="text" name="sql_Connection" value="@ViewBag.GetSqlConnection">
        <span style="color:black">&#160;&#160;&#160;Example:</span> <span style="color:blue"> Server=-</span>
        <br>
        <br>
        <button class="btn btn-success" type="submit" >Save Changes</button>
    </section>
</div>

}

【问题讨论】:

    标签: asp.net-mvc html jquery


    【解决方案1】:

    所以我想出了我的答案。仅供将来尝试此操作的任何人参考,以下是我所做的。我把它放在@using (Html.BeginForm("Admin", "Home", "POST")) 上面。 @Andrew我确实尝试过你的,但我无法让它工作。感谢大家热心帮助我。

    <script language="javascript" type="text/javascript">
        $(function() {
           $("form").submit(function(e) {
              $.post($(this).attr("action"), // url 
      $(this).serialize(), // data
      function (data) { //success callback function
         alert("Edit successful");
      }).error(function () {
          alert('failure'); 
      });
                 e.preventDefault();
              });
           });
    </script>
    

    【讨论】:

      【解决方案2】:

      您需要稍微更改您的 HtmlHelper BeginForm 声明,以便 id 属性将与元素一起呈现,如下所示:

      @using (Html.BeginForm("Admin", "Home", FormMethod.Post, new { id = "well-form" }))
      

      现在您可以在声明上方添加一个脚本,该脚本捕获并提交表单并处理响应(成功或错误)。

      <script>
      $(function() {
          // Find the form with id='well-form'
          $('#well-form').submit(function() {
              $.ajax({
                  url: this.action,
                  type: this.method,
                  data: $(this).serialize(),
                  success: function(result) {
                     alert('Post was successful!');
                  },
                  error: function(result) {
                     alert('Post was not successful!');
                  }
              });
              // return false to cancel the form post
              // since javascript will perform it with ajax
              return false;
          });
      });
      </script>
      

      【讨论】:

        【解决方案3】:

        您可以使用 Ajax 事件吗?比如$.ajaxSuccess

        http://api.jquery.com/category/ajax/global-ajax-event-handlers/

        【讨论】:

          【解决方案4】:

          结合 Sonu 的回答,我发现类似的问题很难完成,因为 HTML 表单提交没有回调,当您想要回发确认您的表单提交时,似乎建议使用 ajax()

          请看这个Stackoverflow Link

          【讨论】:

            【解决方案5】:
            $.ajax({
              url: 'post.html',
              success: function(){
                alert('success');
              },
              error: function(){
                alert('failure');
              }
            });
            

            【讨论】:

            • 如果我使用 HTML 帮助帖子而不是通过 jquery/ajax 发布,我该怎么做?
            • 那么有没有办法做我想做的事?
            • 您编写的代码不适用于问题中的 HtmlHelper 声明,它将呈现为 &lt;form action="/Home/Admin" method="post"&gt;...&lt;/form&gt;
            猜你喜欢
            • 1970-01-01
            • 2010-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多