我的javascript体验非常有限,我试图修改下面的脚本,以便在POST尝试后显示成功或失败的警告:
<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>
我知道它需要看起来像这样:
success: function(){
alert("Edit successful");
},
error: function(){
alert('failure');
但无法弄清楚如何在没有语法错误的情况下集成它。此脚本用于执行上述代码通常执行的POST,以允许确认成功或失败:
@using (Html.BeginForm("Admin", "Home", "POST"))
{
<div class="well">
<section>
<br>
<span style="font-weight:bold">Text File Destination:   </span>
<input type="text" name="txt_file_dest" value="@ViewBag.one">
<span style="color:black">   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">   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();
}
答案 0 :(得分: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');
}
});