Ajax does not return to view using find ()

1

Hello, I'm creating an ajax but if I use find to return in the desired div it does not return

success: function (ret) {
            if (ret == "sucesso") {
                $(this).find(".msg-envio").html("<p class='msg-sucesso'>Orçamento enviado com sucesso!</p>");
            }
            else {
                $(this).find(".msg-envio").html("<p class='msg-erro'>Houve um erro ao enviar o orçamento.</p>");
            }
        }
    
asked by anonymous 05.01.2016 / 18:24

1 answer

3

The this within ajax is in another scope. You need to create a variable to hold this reference because ajax assigns something else to this .

You've put a lot of code so I'll give you an approximate example of your code:

// antes do ajax
var self = this; // <-- aqui guardas a referência
$.ajax({
    // etc
    success: function (ret) {
        if (ret == "sucesso") {
            $(self).find(".msg-envio").html("<p class='msg-sucesso'>Orçamento enviado com sucesso!</p>");
        }
        else {
            $(self).find(".msg-envio").html("<p class='msg-erro'>Houve um erro ao enviar o orçamento.</p>");
        }
    }
// etc...

There is, however, an option to force the use of the this in which ajax is called, in that case, instead of doing as I put it on, add this in the object passed to ajax:

context: this,
    
05.01.2016 / 18:28