0
我已經包含JavaScript代碼數據提交到控制器asp.net Ajax的提交事件顯示從控制器返回的警報
數據在控制器成功處理的視圖,不過頁顯示,而不是JSON結果從控制器返回json結果而不是alert。任何想法 ?
這裏是我的代碼
<code>
@using (@Html.BeginForm())
{
//some thing
<input id="btSaveDetails" type="submit" name="btnSave" value="Save" />
}
javascript
<script type="text/javascript">
$("#btSaveDetails").submit(function (e) {
$.ajax({
type: "POST",
cache: false,
async: true,
url: '@Url.Action("Groups", "User")',
data: $('form').serialize(),
dataType: "json",
success: function(response) {
alert(response.Status);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown == "Forbidden") {
alert("Forbidden access");
} else {
alert('An error occurred please retry.');
}
}
});
});
</script>
controller :
public JsonResult Groups(GroupsModel userGroups)
{
//some processing
return Json(new { Status = "Saved" });
}
</code>
感謝
嘗試'返回JSON(新{狀態= 「已保存」},JsonRequestBehavior.AllowGet);' –
的表單被提交,沒有任何東西阻止默認提交 – adeneo
嘗試將輸入類型更改爲「按鈕」而不是「提交」以防止默認表單提交或提交函數阻止默認事件,如e.preventDe錯誤()並返回false – malkam