1

That's the code:

function serialize(form) {
    var data = new FormData(document.querySelector(form));
    return data;
}

var MakeAjaxConn = function (url, data, action) {
    // Ação para PHP ler
    data.append('action', action);

    // Variáveis auxiliares/acessórias
    var request = new XMLHttpRequest(), result = "";

    request.onreadystatechange = function () {
        if (request.readyState == 4) {
            if (request.status == 200) {
                if (request.responseText.length > 0) {
                    result = request.responseText;
                } else {
                    result = '<p>Nenhum valor retornado.</p>';
                }
            } else {
                result += "<p>Atenção<p>";
                result += "<p>Uma instabilidade provocou um erro. Tente novamente em 5min.</p>";
                result += "<p>As informações abaixo são importantes, tire um print. Se o problema persistir, contate o administrador.</p>";
                result += "<p>ReadyState = " + request.readyStat + "</p>";
                result += "<p>Status = " + request.status + "</p>";
            }

            MakeAjaxConn.prototype.result = result;
        }
    };
    request.open('POST', url, true);
    request.send(data);
};

button.addEventListener('click', function () {
    msg.innerHTML = '<div><img src="' + document.location.origin + '/public/g_imgs/load.svg"></div><div>Por favor, aguarde...</div>';

    data = serialize('form#login');
    var ajaxObj = new MakeAjaxConn('../app/controller/admin/ajax/login.php', data, 'login');
    console.log(ajaxObj);
});

Requisition response at console: enter image description here

I can't getting access "result" value inside proto.

Cœur
  • 37,241
  • 25
  • 195
  • 267
E. Coelho
  • 1
  • 2
  • 1
    Are you sure that the XHR call ended when you click the button? It looks like an async problem – Christian Vincenzo Traina Apr 10 '19 at 14:57
  • Why on earth are you trying to store the result in the prototype??? – GifCo Apr 10 '19 at 14:57
  • Hi @CristianTraìna. Thank You for your colaboration. I have this new project where I'm trying not use libraries. So... I'd like some help. Previouly I used jQuery (simple, but weighty). – E. Coelho Apr 10 '19 at 15:09
  • Hi, @GifCo. In this case, how am I supposed to work? I have to learn with You. Thank You. – E. Coelho Apr 10 '19 at 15:14
  • 1
    @E.Coelho Cristian doesn't mention anything about external libraries. The problem in your code is that JavaScript will continue executing without waiting for a response from the server. So your line `console.log(ajaxObj);` is executed _before_ the `request.onreadystatechange` callback function is ran. The link at the top of your question explains in good detail on why this is and how you can solve it. – Ivar Apr 10 '19 at 15:23
  • Hi, @Ivar. Thank You all. I read the post "How do I return the response from an asynchronous call?" and I filnally understood my problem. About what I wrote - concerning Cristian's comment - I'm sorry. Maybe I made You a wrong impression. I have some dificults with english language, but I like ask in this idioma. I didn't get any good answer in portuguese language. Can You set "Solved" please? I didn't find button for this... – E. Coelho Apr 10 '19 at 20:27
  • @E.Coelho You can't really set questions to solved on Stack Overflow. You can pick one answer as accepted, but that doesn't apply here because there aren't any answers. You can just leave this question as is. – Ivar Apr 10 '19 at 20:58

0 Answers0