Relative URL in JQuery Post Call

前端 未结 7 1837
终归单人心
终归单人心 2021-01-30 17:22

I have the following situation.

I developed my first MVC Asp.Net application. it runs on my server at the following adress

http://localhost:59441/
         


        
7条回答
  •  遥遥无期
    2021-01-30 18:17

    I had this kind issue on MVC 5 using JQuery, so I went to this solution that gets rid of the problem when you are in Localhost, and in any Navigator even when you're deploying app in a subfolder.

    var pathname = window.location.pathname;
    var VirtualDirectory;
    if (pathname.indexOf("localhost") >= 0 && pathname.indexOf(":") >= 0) {
        VirtualDirectory = "";
    }
    else {
        if ((pathname.lastIndexOf('/')) === pathname.length + 1) {
            VirtualDirectory = pathname.substring(pathname.indexOf('/'), pathname.lastIndexOf('/'));
        } else {
            VirtualDirectory = pathname;
        }
    }
    

    And then in any ajax call :

    $.post(VirtualDirectory + "/Controller/Action", { data: data}, "html")
                 .done(function (result) {
                     //some code             
    });
    

提交回复
热议问题