asp.net mvc 3: deployment. getJson urls don't work

断了今生、忘了曾经 提交于 2019-12-24 03:12:48

问题


I have a very simple asp.net mvc3 app that uses jquery::getJSON to call into my controller and get some data top display via jquery::tmpl.

function ajaxError(jqXHR, textStatus, errorThrown) {
    alert(errorThrown);
}
....

    $.ajaxSetup({
        cache: false,
        error: ajaxError // tell me what the error was
    });

    var cl = $("#listcontainer");
    $(cl).empty();
    $.getJSON("/Home/GetSomeData", { oldData: "" }, function (data) {
        $.each(data, function (i, item) {
            var t = $("#listitem").tmpl(item);
            $(cl).append(t);
        });
    });

Everything works ok under IIS express, however When I deploy the app to a freshly setup iis7 on win2k8 r2, the getJSON call fails with the error "Not Found" being displayed via the ajaxError function. (the actual app runs ok otherwise).

I can actually call the action from the browser by typing it in - http://webservername/myapp/Home/GetSomeData - and it returns the json to me.

Is this a configuration error? Or am I not supposed to be doing it like this?

TIA.


回答1:


The issue here is that your URLs are hardcoded and don't contain the virtual directory you're working from.

Rather than hardcode your URLs you should look at using the routing built into MVC.

You can use the Action method of the UrlHelper to generate links for you such as:

Url.Action("GetSomeData","Home")

So that:

$.getJSON(@Url.Action("GetSomeData","Home"),[...]



回答2:


Use the UrlHelper like below to generate the correct URL in both cases:

Url.Action("GetSomeData", "Home")

If using razor this would end up like:

$.getJSON("@Url.Action("GetSomeData", "Home")", { oldData: "" }, function (data) {
    $.each(data, function (i, item) {
        var t = $("#listitem").tmpl(item);
        $(cl).append(t);
    });
});


来源:https://stackoverflow.com/questions/7042294/asp-net-mvc-3-deployment-getjson-urls-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!