0
我一直試圖讓這個工作3個小時。即時通訊做一個jQuery的職位,但一些在線我做錯了什麼,但我沒有得到什麼。jquery post是否是ajaxrequest?
在我的方法我檢查它是否ajaxrequest,如果它是我返回一個字符串消息基於結果。
[HttpPost]
public ActionResult PostJquery(ContactViewModel viewModel)
{
if (Request.IsAjaxRequest())
{
if (!string.IsNullOrEmpty(viewModel.Email))
{
return Content("success");
}
else
{
return Content("Fail");
}
}
else
{
return RedirectToAction("About");
}
}
我的jQuery和html看起來像這樣。
<% using (Html.BeginForm())
{%>
<%: Html.TextBoxFor(model => model.Email) %>
<br />
<br />
<%: Html.TextBoxFor(model => model.Title) %><br />
<input type="submit" value="Save" id="button" />
<%} %>
<script src="<%= Url.Content("~/Scripts/jquery-1.5.2.js") %>" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#button').click(function (e) {
$.post('/Home/PostJquery', { viewModel: $('form').serialize() }, function (data) { alert(data); });
});
});
</script>
它的作用是進入的方法和重定向到該網站有關,因爲它不是一個ajaxrequest,如果我刪除它,它會返回成功,但它帶我到一個空白頁與文本的成功writen。它不會在alert中觸發成功。我可能做錯了什麼?我應該使用.post還是.ajax?
感謝鄉親輸入
謝謝你,我嘗試過函數(e),e.preventDefault();沒有運氣。我總是錯過了文檔中的返回錯誤。你拯救了我的一天Darin。 – 2011-04-16 17:24:10
您是如何指定正確的內容類型的?你能給我一個快速的例子嗎? – 2011-04-16 17:26:24
@ Dejan.S,像這樣:'return Content(「success」,「text/plain」);'或者如果您使用的是Json:'return Json(new {success = true}); – 2011-04-16 17:39:21