我有一個需要通過Ajax回發的應用程序。撥打ActionMethod
時出現問題。下面是代碼。使用ajax調用動作方法時的問題contentType
控制器:
[HttpGet]
public ActionResult Index()
{
return View();
}
public JsonResult AjaxTest(object name)
{
return Json("Welcome" + name, JsonRequestBehavior.AllowGet);
}
Cstml:
<form name="frmTest" method="POST">
@Html.Label("Your name Please")
@Html.TextBox("username")
<input class="btn btn-success btn-lg btn-block" type="submit" value="Login">
JQuery的:
$(function() {
$("form[name='frmTest']").submit(function (e) {
var name = $('#username').val();
$.ajax(
{
type: "POST",
//contentType: "application/json; charset=utf-8",
url: "MyTest/AjaxTest",
dataType: "json",
data: { name:name },
success: function (data, status) {
alert("Pass"+data);
},
error: function() {
alert("Fail");
}
});
});
});
通過使用命中AjaxTest
與上面的代碼中斷點參數。當我使用contentType: "application/json; charset=utf-8",
時,頁面回發沒有達到中斷點。 但這兩種情況下我都得到了失敗警報。無法找到問題,
請問爲什麼你要一直到服務器來簡單地將「Welcome」添加到名稱? – CodingYoshi
你正在嘗試提交表單時發送一些Ajax,我認爲這是一個或其他,而不是在同一時間。 –
@CodingYoshi thnx,這是我的主應用程序的演示應用程序 – madan