locked
AJAX Call to Web API fails since JQuery is ignoring Subdirectory in base Url RRS feed

  • Question

  • User2122855913 posted

    I have a MVC Web Application with some Web APIs.  In the Views I have implemented the Ajax Call to this Web APIs my problem is when I publish my application in a Subdirectory:

    $.ajax({

           url: "/api/ControllerName/Action",

    });

    I have read that when I use / at the beginning of the url it suppose that JQuery will append the base url of my webapplication that is http://localhost/Subdirectory so the full path of the Ajax call should be http://localhost/Subdirectory/api/ControllerName/Action the problem is that instead of this JQuery is making the Ajax call with this Url http://localhost/api/ControllerName/Action without the Subdirectory.     

    Any idea why the sub-directory is being ignored in the base url?  

    Thanks in advance, 

    Luis A.

    Sunday, March 19, 2017 3:07 AM

All replies

  • User-2057865890 posted

    Hi Luis A,

    Luis A

    Any idea why the sub-directory is being ignored in the base url? 

    Maybe you can try the following method.

    In your layout html you can try to add a JS var.

     var baseUrl = '@Url.Content("~/")';

    then on your ajax call:

    $.ajax({
       
        url: baseUrl + "api/ControllerName/Action",
        accepts: 'application/json',
    });

    I hope it will help you.

    Best Regards,

    Chris

    Monday, March 20, 2017 8:02 AM