0

I have very limited javascript experience and Im trying to modify the script below to show an alert of success or failure after the POST attempt:

<script language="javascript" type="text/javascript">
        $(function() {
            $("form").submit(function(e) {
                $.post($(this).attr("action"),
                        $(this).serialize(),
                        function(data) {
                    $("#result").html(data);
                });
                e.preventDefault();
            });
        });
</script>

I know it needs to look like this:

success: function(){
                    alert("Edit successful");
                },
                error: function(){
                    alert('failure');

but cant figure out how to integrate it without syntax errors. This script is for performing the POST that the above code would normally perform to allow for this confirmation of success or failure to occur:

@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.one">
            <span style="color:black">&#160;&#160;&#160;Example:</span><span style="color:blue"> \\pathpart1\\pathpart2$\\</span>
            <br>
            <br>
            <span style="font-weight:bold">SQL Connection String:</span>
            <input type="text" name="sql_Connection" value="@ViewBag.two">
            <span style="color:black">&#160;&#160;&#160;Example:</span> <span style="color:blue"> Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypswd</span>
            <br>
            <br>
            <button class="btn btn-success" type="submit" name="document">Save Changes</button>
        </section>
    </div>
}

ActionResult

public ActionResult Admin(string txt_file_dest, string sql_Connection)
        {
            AdminModel Values = new AdminModel();

            if (txt_file_dest != null)
            {
                Values.SAVEtxtDestination(txt_file_dest);
            }

            if (sql_Connection != null)
            {
                Values.SAVEsqlConnection(sql_Connection);
            }
            ViewBag.one = Values.GetTextPath();
            ViewBag.two = Values.GetSqlConnection();
            return View();
        }
4

1 回答 1

1
$.post(
    $(this).attr("action"), // url
    $(this).serialize(), // data
    function(data) { //success callback function
        alert("Edit successful");
    }).error(function() {
        alert('failure');
    });

这是一个简写的 ajax 函数,你可以写成:

$.ajax({
  type: "POST",
  url: $(this).attr("action"),
  data:  $(this).serialize(),
  success: function(data){
    alert("Edit successful");
  },
  error(function() {
    alert('failure');
  }
});
于 2013-04-25T13:12:49.053 回答