Using jquery to verify POST success

I am trying to write a jquery function that will appear and say that the message was successful! and if not, the message was unsuccessful! How can I use try catch with some jquery input?

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

}

+5
source share
5 answers

So, I understood my answer. Just for future reference to someone trying this, below is what I did. I posted it above @using (Html.BeginForm ("Admin", "Home", "POST")). @ Andrew, I tried, but I could not get it to work. Thanks to everyone for helping me.

<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>
+3
source

You need to slightly modify the HtmlHelper BeginForm declaration so that the id attribute is rendered using this element:

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

script , -- ( ).

<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>
+1
$.ajax({
  url: 'post.html',
  success: function(){
    alert('success');
  },
  error: function(){
    alert('failure');
  }
});
0

Connecting with the answer on Sonu, I found similar questions that are difficult to complete, since submitting an HTML form does not have a callback, and it seems that it is recommended to use ajax()it when you want to receive confirmation of the return submission of your form.

See. This fooobar.com/questions/1154335 / ... .

0
source

Could you use Ajax events? e.g. $ .ajaxSuccess

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

0
source

All Articles