从AJAX调用MVC控制器功能失败 [英] MVC controller function failed calling from AJAX

查看:201
本文介绍了从AJAX调用MVC控制器功能失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题-我从ajax调用cotroller函数,正在显示alert,但它甚至没有进入控制器功能.

I got a problem - I calling cotroller function from ajax, alert is showing but it not even going into controller function.

我该如何解决这个问题?

How could I solve this problem?

这是我的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");
            }
        }

推荐答案

始终使用Url.Action辅助方法来生成url,这样就不会出现Url错误的可能性.

Always use Url.Action helper method to generate url so that there is no chance of Url mistake.

这样做:

 $.ajax({
                        type: "POST",
                      //  async: false,
                        url: '@Url.Action("SetIntroductionData","Introduction")',
                        data: {
                            projectName:'@Session["projectName"]',
                            intro: _intro
                        },  
                        success: function (){
                            alert("success");
                        },
                    });

这篇关于从AJAX调用MVC控制器功能失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆