locked
ajax error when loading base url but works when adding home/Index on the end RRS feed

  • Question

  • User-1312217318 posted

    The base url for my project won't work without Home/Index on the end, it returns an ajax error. How can I ensure that if the base url is entered without Home/Index, that it will work without returning an error?

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

    Ajax:

    "ajax": {
                "url": "../Home/GetTaskLog",
                "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);
                },
            },
    Monday, February 4, 2019 4:02 PM

All replies

  • User-474980206 posted

    your ajax url is incorrect when you don't specify the full url for the hosting page. you should use the url helper:

     "url": "@Url.Action("GetTaskLog","Home")",
    Monday, February 4, 2019 5:29 PM
  • User-2054057000 posted

    In JavaScript with '/' you can go to the root of the application. So you can use this in mapping your URL for .ajax() method of jQuery. See the below code:

    "ajax": {
         "url": "/Home/GetTaskLog",
         //....

    Monday, February 4, 2019 5:50 PM
  • User-474980206 posted

    In JavaScript with '/' you can go to the root of the application. So you can use this in mapping your URL for .ajax() method of jQuery. See the below code:

    "ajax": {
         "url": "/Home/GetTaskLog",
         //....

    this requires the site be deployed to the root.

    Monday, February 4, 2019 6:01 PM
  • User1520731567 posted

    Hi Quektis,

    The base url for my project won't work without Home/Index on the end, it returns an ajax error. How can I ensure that if the base url is entered without Home/Index, that it will work without returning an error?

    Do you mean you delete this line:

    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    

    If so,the compiler does not recognize the URL according to the {controller}/{action}/{id} rules.

    id = UrlParameter.Optional can let id could null.

    So,../Home/GetTaskLog is not work.

    If you must delete default line, you could modify the url in RouteConfig,like:

    url: "{controller}/{action}/{id}",
    //defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },

    or 

    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },

    More details about route,you could refer to:

    https://www.asp.net/mvc/overview/controllers-and-routing

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/creating-a-route-constraint-cs

    Best Regards.

    Yuki Tao

    Tuesday, February 5, 2019 7:50 AM
  • User-1312217318 posted

    @bruce:  The ajax is used to retrieve data from a database and to populate a dataTable table. In this case GetTaskLog is refering to a function that retrieves data from a database. I can confirm that it works, but only when adding home/index to the end of the url. I am still getting an ajax error with the above code

    @yogyiyogi: Thanks, that still doesn't work though.

    @Yuki tao: Thanks for the input but the changes didn't make any difference, I am still getting an ajax error when loading the base url.

    Tuesday, February 5, 2019 9:11 AM
  • User1520731567 posted

    Hi Quektis,

    but only when adding home/index to the end of the url.

    Actually,I can't quite understand this mean.

    Is my previous understanding correct?

    And what is your ajax  error message?

    Best Regards

    Yuki Tao

    Wednesday, February 6, 2019 6:15 AM