使用jQuery AJAX的ASP.Net MVC路由问题

Jon*_*Jon 3 c# asp.net-mvc jquery routing asp.net-mvc-3

我的页面是domain.com/home/details/1

在我的jQuery AJAX调用中,我有以下内容,但当它调用它调用domain.com/home/details/home/getdata时

我该怎么做才能让它得到妥善解决?

$(document).ready(function () {

            oTable = $('#example').dataTable({
                "bServerSide": true,
                "sAjaxSource": "Home/GetData/",
                "bProcessing": true,
                "bPaginate": true,
                "sPaginationType": "full_numbers",
                "bFilter": true,
                "bAutoWidth": false,
                "fnServerData": function (sSource, aoData, fnCallback) {
                    /* Add some extra data to the sender */
                    //aoData.push({ "filtervalue": $('#filtervalue').val(), "Options": $('#Options').val() });
                    $.getJSON(sSource, aoData.concat($('form').serializeArray()), function (json) {
                        /* Do whatever additional processing you want on the callback, then tell DataTables */
                        fnCallback(json)
                    });
                }
            });

        });
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 17

在ASP.NET MVC中处理URL时,绝对总是使用URL帮助程序.绝对不会像你那样对网址进行硬编码.

所以:

"sAjaxSource": "@Url.Action("GetData", "Home")"
Run Code Online (Sandbox Code Playgroud)

如果这是在一个单独的javascript文件中,您可以在以下位置使用HTML5 data-*属性#example:

<div id="example" data-url="@Url.Action("GetData", "Home")">
    ...
</div>
Run Code Online (Sandbox Code Playgroud)

然后在你单独的js中你可以使用这个.data()方法:

"sAjaxSource": $('#example').data('url')
Run Code Online (Sandbox Code Playgroud)

  • +1表示帮助者.如果您在虚拟目录中部署应用程序(例如http://domain.com/myapp/),则使用`/ Home/GetData`将无法正常工作. (2认同)