XMLHttpRequest - response blank

2

I have a file on the server that, if I find the login and password informed, returns 1 or 0 if nothing is found. However, I am not able to receive this information using XMLHttpRequest . In PHP, I use echo (json_encode($)) to pass the value, but I do not know how to get this value.

Here's my Javascript:

function logar()
{
    var request = new XMLHttpRequest();
    var url = "http://clleyton.hostoi.com/form_login.php/?login=" + document.getElementById("login").value + "&senha=" + document.getElementById("senha").value;
    console.log(url);
    request.open("GET", url, true);
    request.onreadystatechange = function() {
        if (request.readyState == 4)
        {
            if (request.status == 200 || request.status == 0)
            {

                console.log(request.responseText);
                console.log(request.response);
            }
        }
    }

}
    
asked by anonymous 23.03.2016 / 22:38

1 answer

0

I confess that I have no experience with XMLHttpRequest and I can not give you an exact answer on how to solve the problem the way you need it. But I recommend using fetch for communication between client <-> server .

In a simple way you will be able to make the request ( GET ) you need and control the response through Promises .

I know that it may seem too much to use a polyfill for your problem, but in general context I think it is valid to evaluate that it is a new technology with a native specification in javascript. You will only be anticipating what will one day be common day to day. Much more elegant and simple than this XMLHttpRequest ;)

    
24.03.2016 / 05:15