Ajax works locally, but does not work on the server

Asked

Viewed 1,955 times

2

I have an application on Asp.net mvc 5 where the code below works locally (visual studio 2012), but does not work after the publication on the server.

controllerAction = "/Controller/Action/"
controller = "/Controller/"

$('#approve-btn').click(function () {
$.ajax({
    type: "POST",
    url: controllerAction,
    data: { id: idElementoClicado },
    datatype: "html",
    success: function (data) {
        $("#ajaxgrid").load(controller + ' #ajaxgrid', function () {
        });

    },
    error: function () {
        alert(response);
    }
});

$('#modal-container').modal('hide');
});

[HttpPost]
public ActionResult Excluir(CentroCustoViewModel viewModel)
{
  //bloco de execução

  return Json(datasource, JsonRequestBehavior.AllowGet);
}

By clicking the trigger button the action always falls in the error event and emits the Alert.

Does anyone know what might be going on?

Thank you

3 answers

3

In his <body> of Masterpage put it like this:

<body data-base="http://www.dominio.com.br/">

In data-base would be your main LINK.

Then create a global variable (or not) in JS:

var baseURL;

And then:

baseURL = $('body').data('base');

Then here in the path variables put it:

controllerAction = baseURL + "/Controller/Action"
controller = baseURL + "/Controller"

Another thing, take the / end of URL when POST.

  • Worked Not Zoom. :(

  • What should your Localhost LINK look like when you call this function and what domain are you using on the host ? I edited my post with a change.

  • 1

    I removed the '/' from the end and continued the same thing (it worked locally and did not work on the server). The link is mounted correctly http://server/nameserver/controller/action/Id

  • See the Inspect Element error in Chrome.

  • This is the problem, not the error. The action does not happen.

0

A palliative solution to this situation would be to name a variable/constant with its domain.

For example:

var siteRoot = 'www.meusite.com.br/';

the palliative is precisely in the content of this variable, because this way will work only on the server/ hosting. When you want to use local, you must enter the local server prefix. for example:

var siteRoot = 'localhost/meuprojeto/';

Another (not very organized) way is to describe the path from the file.

To understand the example, let’s simulate a project folder structure:

/raiz
--/imagens
----------/logo
---------------/logo_principal.png
----------/imagem00.png
----------/imagem01.png
----------/imagem02.png
--/codigos_css
----------/style.css
--/codigos_javascript
----------/functions_ajax.js
--/classes
----------/bd.class.php
----------/crud.class.php
--/log
----------/log.txt
--/index.asp
--/produtos.asp

Now suppose the location you call the Jquery function that will connect via Ajax is the 'functions_ajax.js' and its intention is to access the contents of the PHP file 'bd.class.php', found inside the folder 'classes'.

The walk to access the desired file, obeying the folder structure, would be:

../classes/bd.class.php

soon

$.ajax({
  type: "POST",
  url:'../classes/bd.class.php',
  data: { id: idElementoClicado },
  datatype: "html",
  success: function (data) {
    $("#ajaxgrid").load(controller + ' #ajaxgrid', function () {
    });

  },
  error: function (jqXHR,msgError) {
    alert(msgError);
  }
});
  • thelimarenan, the way is correct the problem is that there is an action between the front-end and the back-end.

  • I noticed something, but I don’t know if it’s good for something: Take a look at the Jquery Error condition. By default it returns three parameters: " jqXHR jqXHR, String textStatus and String errorThrown " Try to put at least two of them to Test Obs: just like in my answer [edited]

  • Good morning Thelimarenan, I discovered the mistake but I still don’t know how to solve it. The application is called by the url: http://server and of course the application is called by the url: http://server/application. The application didn’t work, it was when I searched and saw that I should tag the web.config: <system.webserver> <rewrite> <Rules> <clear /> </Rules> </rewrite> </system.webserver> I changed the Ajax error to: error: Function (Response) { Alert(JSON.stringify(Response)); } .

  • Just don’t forget that the first parameter returns the error code, enter a second parameter to see the Error String.ex: error: Function (Response,errortext) { Alert(JSON.stringify(errortext)); }

  • Response returns an html (it was lah I saw that there are many web site references). errortext returns me only a string 'error'. There’s something else to change?

0

I got it solved!! The problem is that the

System.Web.Helpers.WebGrid 

was killing the ajax, so got lost in the way.

I changed the ajax url to:

'@Url.Action("NomeDaAction","NomeDoController")'

and removed the following line before @grid.Gethtml(...)

 using ((Html.BeginForm()))
 { 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.