有没有理由说明为什么在MVC 1中工作的ajax不会出现在MVC3中?

时间:2011-03-10 13:37:25

标签: c# jquery asp.net-mvc ajax asp.net-mvc-3

喂,

我最近将我的项目从ASP.NET MVC 1 .NET 3.5 VS2008升级到ASP.NET MVC 3 .NET 4.0 VS2010。

除了我发现我运行的ajax的某个特定部分不再有效之外,其中大部分都没有问题。

以下是代码:

    var filterEntities = function () {
        $.get({
            url: "../../ProjectEntities.mvc/OfType/<%= Model.Change.Job.Quote.Project.Id %>?entityType=" + $("#ChangesForm select[name=ProjectEntityType]").val(),
            success: function (data) {
                response = projectSupport.parseJson(response);

                var entitySelect = $("#ChangesForm select[name=ProjectEntity]");
                entitySelect.empty();

                hasValues = (response.length > 0);

                for (var i in response) {
                    entitySelect.appendListItem(response[i].id, response[i].title);
                }

                updateEditLink();
            }
        });
    }

该代码继续调用

public ActionResult OfType(int id, int entityType)
    {
        var project = projectService.Find(id);
        return Json(projectEntityService.ProjectEntitiesOfType(applicationService.ForProject(project), (EntityType)entityType).Select(entity => new { title = entity.Title + " (" + entity.Application.Description + ")", id = entity.Id }));
    }

以前都很好用。任何人都有任何想法可能导致问题?我在网站的其他部分有ajax工作正常,所以我不认为我丢失了适当的jquery文件或任何东西。

谢谢, 哈利

2 个答案:

答案 0 :(得分:6)

您需要在JsonRequestBehavior.AllowGet

上设置return Json()
var data = projectEntityService.ProjectEntitiesOfType(applicationService.ForProject(project), (EntityType)entityType).Select(entity => new { title = entity.Title + " (" + entity.Application.Description + ")", id = entity.Id });

return Json(data, JsonRequestBehavior.AllowGet);

这样做是为了防止Json Hijacking

答案 1 :(得分:2)

您需要允许默认情况下禁用的GET请求,以便从ASP.NET MVC 2开始返回JSON:

return Json(
    projectEntityService.ProjectEntitiesOfType(applicationService.ForProject(project), (EntityType)entityType).Select(entity => new { title = entity.Title + " (" + entity.Application.Description + ")", id = entity.Id }), 
    JsonRequestBehavior.AllowGet
);