Function does not enter ajax success with jquery

Asked

Viewed 392 times

0

I cannot enter the success of ajax with this function. The function works, that is, calls the double click. The first Alert is ok, but the second is not fired.

$('#nmUsuario').on("dblclick", '.clique', function() {

    alert('Alô, tudo bem?');

    var obj = {};

    $('tr').each(function () {
        obj = {
            _nivel: $(this).find('td').eq(0).text, 
            _nome: $(this).find('td').eq(1).text, 
            _usuario: $(this).find('td').eq(2).text
        }
    })

    $.ajax({

            url: '/CadastroAcesso/CarregaDadosPagina',
            datatype: 'json',
            contentType: 'application/json;charset=utf-8',
            type: 'POST',
            data: obj,
            success: function(data){
                alert('Alô, tudo bem 1?');
            },
            error: function(error){
            }
        })
})

Doing my tests, I saw that this is wrong:

$('tr').each(function () {
        obj = {
            _nivel: $(this).find('td').eq(0).text, 
            _nome: $(this).find('td').eq(1).text, 
            _usuario: $(this).find('td').eq(2).text
        }
    })
  • 1

    Is the error function called? There are several ways an ajax call fails, and without knowing what the /CadastroAcesso/CarregaDadosPagina makes it hard to know.

  • 1

    Another thing: you define Content-Type as JSON, but the data is sent as an object (which by default is serialized as form data). Exchange the data: obj for data: JSON.stringify(obj). And see if the error function is called with more information on why the success is not.

2 answers

1

Use .text()

dataType can be JSON without problems.

Your obj variable is a problem. It will always contain the data of the last line. Use:

var obj = [];
$('tr').each(function(index) {
   obj[index] = { ...... }
});
  • I cannot pass the value of <td> to my variables in the controller.

0

Friend your problem is here:

 url: '/CadastroAcesso/CarregaDadosPagina',

You need to pass the full url, example:

 url: 'http://www.seusite.com.br/CadastroAcesso/CarregaDadosPagina',

Also make sure that the type of data that is passed in the parameter is returned to this function dataType

Browser other questions tagged

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