Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative URL in JQuery Post Call

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/

I wrote some JQuery Post Methods that looked like this

  $.ajax({
        type: "POST",
        url: "/CeduleGlobale/UpdateCheckBox", ...

CeduleGlobale is my ControllerName and UpdateCheckBox is my methodName

When I put the Application on the testServer, it was put in a VirtualDirectory

hence the application is now

http://testServer/JprApplication/

no more port to specify and also an application Name

When I started to test, I quickly noticed my JQuery Post calls didn't work anymore...

I modified them so now the URL is

/JprMvc/CeduleGlobale/UpdateCheckBox

the problem is 2 fold.

  1. this makes it hard to test on my development machine because IIS Express doesn't allow me to specify a virtual Directory.
  2. I don't like hardCoding the Virtual Directory Name in the JQuery because I dont know what name the Application will have in the production environment and therefore i will have to modify my script before i can install the application in production.

I am sure I am missing some basic thing to simplify this.

Thanks

like image 503
SerenityNow Avatar asked Aug 20 '13 14:08

SerenityNow


Video Answer


3 Answers

Depending on where you actually have your JavaScript located (inside the View or a separate JS file), you have a couple of options.

Option 1 - Inside the View

Simply use the Html Helpers to generate the links for you

<script type="text/javascript">    $(function(){         $.ajax({            type: "POST",            url: "@Url.Action("UpdateCheckBox", "CeduleGlobale")"         });    }); </script> 

Option 2 - Standalone JS File

We typically have a function per page that sets up that page's handlers. So, we can do something like the following:

View

<script type="text/javascript">     $(function(){         SetOrderPage('@Url.Action("UpdateCheckBox", "CeduleGlobale")');     }); </script> 

Standalone JS File

function SetOrderPage(ajaxPostUrl){        $.ajax({            type: "POST",            url: ajaxPostUrl        )}; } 

Option 3 - Standalone JS file Method 2

You could have a global variable in your in your JS file that is the siteroot. The draw back here is that you will need to hand create each of your action method paths. On each page, you could set the site root global variable as such:

Standalone JS File

var siteRoot; 

View

<script type="text/javascript">     siteRoot = '@Request.ApplicationPath'; </script> 

Keep in mind you cannot use Razor syntax in a stand alone JS file. I believe that it is best to let Razor/MVC/.NET dynamically give you the site path or URL route as it will really cut down on the mistakes that could be made when moving between sites/virtual directories.

like image 141
Tommy Avatar answered Sep 20 '22 05:09

Tommy


As far as I know there is no other way around this. Unless you are willing to user relative URL's i.e:

$.ajax({
        type: "POST",
        url: "./CeduleGlobale/UpdateCheckBox", ...

But that can get messy for various reasons when you refactor code. Alternatively prepend the URL which is globally defined and therefore you then only need to change it in once place before going to production.

i.e.

//Globally defined serverRoot
serverRoot = "http://someaddress/somevirtualdirectory";

$.ajax({
        type: "POST",
        url: serverRoot + "/CeduleGlobale/UpdateCheckBox", ...

That way if you don't need it you can just set serverRoot = ''; and all will be back to how it is now.

like image 30
Rob Schmuecker Avatar answered Sep 19 '22 05:09

Rob Schmuecker


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             
});
like image 40
RizzCandy Avatar answered Sep 21 '22 05:09

RizzCandy