简体   繁体   English

Parse.com javascript登录

[英]Parse.com javascript login

Hi Stackoverflow members, 您好Stackoverflow成员,

I started learning javascript a couple days ago but was stumped on one of the problems i came across. 我几天前开始学习javascript,但却遇到了我遇到的一个问题。

Form: 形成:

<form class="login-form" method="post" onsubmit="return tryLogin(this);">
    <h3 class="form-title">Login to your account</h3>
    <div class="alert alert-danger display-hide">
        <button class="close" data-close="alert"></button>
        <span>
             Enter any username and password.
        </span>
    </div>
    <div class="form-group">
        <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
        <label class="control-label visible-ie8 visible-ie9">Username</label>
        <div class="input-icon">
            <i class="fa fa-user"></i>
            <input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username" id="username"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label visible-ie8 visible-ie9">Password</label>
        <div class="input-icon">
            <i class="fa fa-lock"></i>
            <input class="form-control placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password" id="password"/>
        </div>
    </div>
    <div class="form-actions">
        <label class="checkbox">
        <input type="checkbox" name="remember" value="1"/> Remember me </label>
        <button type="submit" class="btn green pull-right">
        Login <i class="m-icon-swapright m-icon-white"></i>
        </button>
    </div>
</form>

Javascript: 使用Javascript:

<script type="text/javascript">
    jQuery(document).ready(function() {     
      App.init();
      Login.init();
    });

    function tryLogin(form) {

        var username = form.username.value;
        var password = form.password.value;

        console.log("Username: " + username + " Password: " + password);
        Parse.User.logIn(username, password, {
            success: function(user) {
                window.location.href="dashboard.html";
                //self.undelegateEvents();
                //delete self;
                alert("Sucess");
                return true;
                },
            error: function(user, error) {
                window.location.href="login.html";
                alert("Error: " + error.code + " " + error.message + username + password);
                return false;
                }
        });

    }
</script>

The problem is that when I attempt to login I get a 100 XMLHttpRequest failed error. 问题是,当我尝试登录时,我得到一个100 XMLHttpRequest失败错误。 This only happens when I input data into the form and click log in. If i don't enter any data and click login, it correctly attempts to login. 这只发生在我将数据输入表单并单击登录时。如果我没有输入任何数据并单击登录,则会正确尝试登录。 If i put the username + password directly into the javascript it also works - but doesn't through the form. 如果我将用户名+密码直接放入javascript它也可以 - 但不通过表单。

Console.log, logs the correct username and password being inputted, but the Parse.User.logIn(username, password) doesn't want to pass it in... but it works if I fill it in like this: Console.log,记录输入的正确用户名和密码,但Parse.User.logIn(用户名,密码)不想传入...但是如果我这样填写它就可以了:

Parse.User.logIn("myemail@email.com", "mypassword", {....

Any help would be appreciated. 任何帮助,将不胜感激。

Cheers! 干杯!

Edit: Here's the error 编辑:这是错误

XMLHttpRequest failed: {"statusText":"","status":0,"response":"","responseType":"","responseXML":null,"responseText":"","upload":{"ontimeout":null,"onprogress":null,"onloadstart":null,"onloadend":null,"onload":null,"onerror":null,"onabort":null},"withCredentials":false,"readyState":4,"timeout":0,"ontimeout":null,"onprogress":null,"onloadstart":null,"onloadend":null,"onload":null,"onerror":null,"onabort":null} 

Could it be a MAMP issue? 这可能是一个MAMP问题吗? PS: Creating new objects etc. works fine. PS:创建新对象等工作正常。

As you dont call preventDefault on the submit event, what happens is that it posts form when you click which has for effect to cancel the ajax call Parse.User is making. 因为你没有在提交事件上调用preventDefault,所以当你点击它时,它会发布表格,这有效取消了Parse.User正在制作的ajax调用。

Add event.preventDefault(); 添加event.preventDefault(); in your event handler and you should be fine. 在你的事件处理程序中,你应该没事。

<form class="login-form" method="post" onsubmit="tryLogin(this); event.preventDefault();">

Or better use jQuery to manage this event 或者更好地使用jQuery来管理这个事件

$(".login-form").submit (function(event) {
    ...
    event.preventDefault();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM