Function Jquery can not find the Controller when I go to the server

1

During the development the function below finds the Control and makes the request correctly, however after publishing on the server the function no longer finds the Controller:

During development I have to leave it like this:

url: "/Controller/Action",

When I upload to the server I have to change it and leave it like this:

url: "/MeuSite/Controller/Action",


function carregaComboEmpresa() {
    $.ajax({    
        //url: "/MeuSite/Controller/Action",       
        url: "/Controller/Action",
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        processData: false,
        data: null,
        success: function (data) {          
        },
        error: function (result) {
        }
    });
};
    
asked by anonymous 22.06.2017 / 16:33

3 answers

3

It's a bad idea to use this this way.

There are already workarounds for this, use Url.Action

function carregaComboEmpresa() {
    $.ajax({       
        url: '@Url.Action("Action", "Controller")',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        processData: false,
        data: null,
        success: function (data) {          
        },
        error: function (result) {
        }
    });
};
    
22.06.2017 / 16:37
2

It happens because you are calling your url with an absolute path (since it starts with / ). there are 2 simple ways to avoid this:

Make your development environment the same structure as your production environment, in case if you are using MAMP or similar, simply place your entire project inside a directory called MeuSite and point the server to the directory containing MeuSite .

Another way is to use a relative url, which would depend on where your .js se encontra file, for example

../Controller/Action

That indicates that the Url should go down one level and enter the Controller directory. this will work for your .js you are in

MeuSite/js/script.js

for example.

    
22.06.2017 / 16:42
1

This is because the paths to the application on the server and in your debug environment are different, as you yourself made clear in the question.

When you are requesting your controller via Javascript, you need to get the correct address. My suggestion is as follows, in two parts:

  • On each page of the system, you enter an input with the correct path of the application. To not replicate code, you can put this one time in your _layout.cshtml :
<input type="hidden" id="caminho" value="@Url.Content("~/")" />

This @Url.Content("~/") , when interpreted by Razor, returns the path to the root of your application, complete. It's worth testing and checking.

  • At the time of calling the controller via Javascript, your URL looks like this:
var url = $("#caminho").val() + "controller/action"; // o caminho já inclui um "/".

Here you can use the url variable as the path to an ajax request, that is:

$.ajax({
    url: url,
    type: "post"
    // etc., etc.
})

Doing so, you do not need to mix Razor code with Javascript code.

    
22.06.2017 / 16:41