开发者

Is there any reason why ajax that worked in MVC 1 wouldn't in MVC3?

Hullo,

I recently upgraded my project from ASP.NET MVC 1 .NET 3.5 VS2008 to ASP.NET MVC 3 .NET 4.0 VS2010.

Most of it has gone alright except I've found that a particular part of ajax I run no longer works.

Here is the code:

    var filterEntities = function () {
        $.get({
            url: "../../ProjectEntities.mvc/OfType/<%= Model.Change.Job.Quote.Project.Id %>?entityType=" + $("#ChangesForm select[name=ProjectEntityType]").val(),
            success: function (dat开发者_JAVA百科a) {
                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();
            }
        });
    }

That code goes on to call

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 }));
    }

which all worked nicely before. Anyone have any ideas what could be causing the problem? I have ajax on other parts of the website that are working fine so I don't think I lost the appropriate jquery files or anything.

Thanks, Harry


You need to set the JsonRequestBehavior.AllowGet on your 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);

This was done to prevent Json Hijacking


You need to allow GET requests which are disabled by default for action returning JSON starting from ASP.NET MVC 2:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜