1

Can't figure out why the login is not working. Here's the javaScriptcode:

Parse.initialize("app id", "app key");

function fazerLogin() {
    var username = document.getElementById('usuario').value;
    var password = document.getElementById('senha').value;

    alert(username);
    alert(password);

    Parse.User.logIn(username, password, {
        success: function(user) {
            alert("deu certo");
            // Do stuff after successful login.
        },
        error: function(user, error) {
            alert("erro");
            alert(user + error);
            // The login failed. Check error to see why.
        }
    });
}

var botao = document.querySelector('#botao');
botao.onclick = fazerLogin;

The function gets called because I see the alerts with the correct login and password. I can login in iOS sdk with no problem, but it doesn't work on javaScript, I'm not sure what I'm missing. At the end of htmldocument inside body I have:

     <!-- PARSE.COM JS -->
    <script src="assets/js/parse-1.2.18.js"></script>
    <script src="assets/js/login.js"></script>

Even if I put the username and password as strings, like: Parse.User.logIn("username", "password", {, it still doesn't work.

UPDATE:

The login works when I call it outside function:

Parse.User.logIn("email", "PASS", {
        success: function(user) {
            console.log("Deu certo");
            console.log(user);
            return true;
        },
        error: function(user, error) {
            console.log(user + error);
            return false;
        }
    });

HTML form

<form role="form" id="formulario-login">
                <div class="form-group has-feedback lg left-feedback no-label">
                  <input type="text" class="form-control no-border input-lg rounded" placeholder="Usuário" autofocus id="usuario">
                  <span class="fa fa-user form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback lg left-feedback no-label">
                  <input type="password" class="form-control no-border input-lg rounded" placeholder="Senha" id="senha">
                  <span class="fa fa-unlock-alt form-control-feedback"></span>
                </div>
                <div class="form-group">
                  <div class="checkbox">
                    <label>
                      <input type="checkbox" class="i-yellow-flat"> Manter-me conectado.
                    </label>
                  </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-warning btn-lg btn-perspective btn-block" id="botao">LOGIN</button>
                </div>
            </form>
10
  • Can we assume that the app id and app key strings are correct on your end? Also, when you're saying it doesn't work, what do you mean? Does it end up in the error block? Does it alert you of an error message? Give you an exception? Commented May 18, 2014 at 14:12
  • App keys are correct. I just get [object Object][object Object]on console log. that call is in the login error. So I believe the login function executes. Commented May 18, 2014 at 14:23
  • instead of alert I have console.log(error), that is different in the code above. sorry. Commented May 18, 2014 at 14:25
  • 1
    In that case, what has to be happening is that when you're retrieving data via document.getElementById('usuario').value;, the data isn't clean. Could you add the HTML for those two elements as well? Commented May 18, 2014 at 14:52
  • 1
    Try changing your button type value to this: <button type="button" instead of <button type="submit" Commented May 18, 2014 at 15:06

1 Answer 1

7

As we discussed in the comments, the problem was that you were using <button type="submit" instead of <button type="button".

The core difference between the two is that the submit type will actually submit the form and the data you have in your HTML form before executing your function, whereas the button type will give you a button that does not submit, but will fire all events assigned to it (such as your onclick).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.