MVC + DataTables: Ajax error when loading base url. Works fine when adding home/Index to url

MVC + DataTables: Ajax error when loading base url. Works fine when adding home/Index to url

JoeJoeJoeJoeJoeJoe Posts: 50Questions: 13Answers: 1
edited February 2019 in Free community support

I am getting an ajax error when I try to load the base url for my MVC 5 application that uses DataTables. However, it works fine when I add Home/Index to the end of the url. How can I ensure that the base url works without throwing an ajax error?

Ajax:

            "ajax": {
                "url": "../Home/GetTaskLogList",
                "type": 'POST',
                "content-type": "application/json; charset=utf-8",
                "processData": false,
                "dataType": "json",
                "headers": "headers",
                "data": "window.JSON.stringify(obj)",
                //"headers": { "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val() },
                //"data": { "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val() },
                "traditional": true,
                "dataSrc": function (data) {
                    return JSON.parse(data);
                },
            },

Route config:

     public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    If you don't add Home/Index what does the server response with? It will depend upon the routing in your application and how it is setup if it needs that or not. I'm afraid that is out of my depth though - you'd need to ask on SO or a .NET specific forum about that.

    Allan

  • JoeJoeJoeJoeJoeJoe Posts: 50Questions: 13Answers: 1
    edited February 2019

    Actually the base url (localhost/WTM/) works with just /Home on the end as well as with /Home/Index. The server responds with a 404 not found error for GetTaskLogList. GetTaskLogList is requesting localhost/Home/GetTaskLogList whereas the base url is localhost/WTM/. If I set WTM as my controller and Home as the action in my routing, I get a "forbidden" error

  • JoeJoeJoeJoeJoeJoe Posts: 50Questions: 13Answers: 1
    edited February 2019 Answer ✓

    The solution to my problem was to change the ajax url to:

     "ajax": {
                 "url": "/WTM/Home/GetTaskLog"
                }
    
This discussion has been closed.