Ajax works locally, but does not work on the server

2

I have an application in asp.net mvc 5 where the code below works locally (visual studio 2012), but does not work after posting to 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);
}

Clicking on the button that triggers the action always falls on the error event and issues the alert.

Does anyone know what might be happening?

Thank you

    
asked by anonymous 27.01.2016 / 19:09

3 answers

3

In your%% of MasterPage put it like this:

<body>

In <body data-base="http://www.dominio.com.br/"> would be your main LINK.

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

data-base

And then:

var baseURL;

Then here in the path variables put it:

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

Another thing, it takes the baseURL = $('body').data('base'); from the end of the URL when by POST .

    
27.01.2016 / 19:12
0

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

For example:

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

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

var siteRoot = 'localhost/meuprojeto/';

Another way (not very organized) 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

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

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

../classes/bd.class.php

logo

$.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);
  }
});
    
27.01.2016 / 20:34
0

I managed to solve it !!! The problem is that

System.Web.Helpers.WebGrid 

It was killing ajax, so it got lost on the way.

I changed the ajax url to:

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

and I removed the following line before @ grid.GetHtml (...)

 using ((Html.BeginForm()))
 { 
    
28.01.2016 / 19:23