调用我有一个奇怪的问题。这是我的jQuery代码阿贾克斯提交自动MVC
$("#btnRate").click(function (e) {
alert("tık");
e.preventDefault();
var electionId = '@Model.ElectionId';
var profileId = '@Model.ProfileId';
$.ajax({
url: "Profile/Vote", // '@Html.Action("Vote","Profile")',
// data: { electionId: electionId, profileId: profileId },
dataType: "json",
type: "POST",
error: function (error) {
alert('An error occured, please try again! ');
},
success: function (data) {
if (data != null && data.success) {
alert("s1");
alert(data.url);
alert("s2");
window.location = data.url;
} else {
alert('An error occured, please try again. ');
}
}
});
return false;
});
,这是HTML端代码
时加载详表<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="btnRate" type="submit" class="btn btn-default" value="Vote !" />
</div>
</div>
,自动被调用为点击btnRate按钮。但我不点击..
,这是ProfileController可
// [HttpPost]
[ChildActionOnly]
public JsonResult Vote() //(int profileId, int electionId)
{
EvoteServicesProviderClient service = new EvoteServicesProviderClient();
// var result= service.createPolls(electionId, profileId);
// if(result ==1)
// return Json(new { success = true, url = "/Home/ProfileStatistic?electionId=" + electionId }, JsonRequestBehavior.AllowGet);
// else
// return Json(new { success = false, url = "/Home/ProfileStatistic?electionId=" + electionId }, JsonRequestBehavior.AllowGet);
return null;
}
即使我不点击,投票功能是通过AJAX调用投票行动。什么原因呢?
编辑:这是例外
类型“System.Web.HttpException”发生在 System.Web.dll中但在用户代码
附加信息没有处理的一个例外:错误执行子请求处理程序 'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'。
确保你是**不使用'@ Html.Action()**'在Ajax请求的url,因为当我们使用'@Html.Action出现此问题'而不是尝试使用'@ Url.Action' – 2014-12-04 08:16:17
它标有'[ChildActionOnly]',所以它不能被ajax m调用ethod,所以你把它叫做别的地方。 – 2014-12-04 08:17:16
@Kartikeya,我用/后/ ..”是一样的吗? – unbalanced 2014-12-04 08:20:09