我遇到了一个问题 - 我从ajax调用了cotroller函数,alert
正在显示,但它甚至没有进入控制器函数。
我怎么能解决这个问题?
这是我的ajax功能
$('#save').on('click', function() {
if (validateForm("intro2d") && validateForm("intro3d") && validateForm("text_cs")) {
intro_data = {
projectId: "@Session["projectId"]",
Image3d: "", Text3d: document.getElementById("intro3d").value,
Image2d: "", Text2d: document.getElementById("intro2d").value,
ImageCs: "", TextCs: document.getElementById("text_cs").value
};
var _intro = JSON.stringify(intro_data);
$.ajax({
type: "POST",
// async: false,
url: 'Introduction/SetIntroductionData',
data: {
projectName:"@Session["projectName"]",
intro: _intro
},
success: function (){
alert("success");
},
});
}
});
继承人:
[HttpPost]
public ActionResult SetIntroductionData(string projectName, Intro intro){
using (ISession session = NHibernateHelper.OpenSession()){
var exists = session.Query<Intro>().SingleOrDefault(x => x.Project.Id == intro.Project.Id);
if (exists != null){
return Json("exist");
}
session.Save(intro);
// return Redirect(Url.Action("Index", "Data", new { projectId = project.Id }));
return Json("success");
}
}
答案 0 :(得分:1)
始终使用Url.Action
辅助方法生成网址,以便不会错误地记录网址。
这样做:
$.ajax({
type: "POST",
// async: false,
url: '@Url.Action("SetIntroductionData","Introduction")',
data: {
projectName:'@Session["projectName"]',
intro: _intro
},
success: function (){
alert("success");
},
});
答案 1 :(得分:1)
此外,您可以使用fiddler或dev工具检查那里发生的事情并查看服务器的响应。有时即使发生错误,您也可能会看到代码200,但在响应文本中会出现错误描述。我怀疑你需要明确设置contentType: "application/json; charset=utf-8",
以便服务器被指示内容类型,asp.net将正确解析匹配控制器的操作及其参数
答案 2 :(得分:1)
IMO必须在模型绑定中失败(将_intro转换为Server的Intro类对象)。您可以通过在客户端和服务器操作上指定简单的字符串参数来确认。
确认后,您可以深入查看哪个现场服务器无法映射,或者我只想编写自定义模型绑定器。请查看以下链接以了解有关模型绑定的更多信息:
6 Tips for ASP.NET MVC Model Binding
另外按照@Borys Generalov给出的建议,您可以在浏览器本身点击网址时找到确切的问题。我使用Chorme浏览器,按F12,转到网络选项卡,刷新页面,选择网址,然后单击右侧面板上的响应选项卡。它将为您提供服务器返回的详细信息。
答案 3 :(得分:1)
您应该尝试将您的网址和数据值更改为:
url: '../Introduction/SetIntroductionData',
data: 'projectName=' + projectName + '&intro='+ _intro,