简体   繁体   English

e.preventdefault和asp.net登录表单不起作用

[英]e.preventdefault and asp.net login form not working

I am trying to submit the asp.net login form only if the https.get function does not produce an error. 我试图仅在https.get函数不会产生错误的情况下提交asp.net登录表单。 If it returns an error, I do not want the login form submitted. 如果返回错误,我不希望提交登录表单。 When I call e.preventDefault() the login page just refreshes without logging the user in. 当我调用e.preventDefault()时,登录页面只是刷新而无需登录用户。

Why is this? 为什么是这样?

var loginForm = $('form');
var https = require("https");
var url = 'https://examplesite.com/';


loginForm.on('submit', function (e) {
        var loginSubmit = this;
        e.preventDefault();
            https.get(url, function (res) {
                res.resume();
                loginSubmit.submit();
            }).on('error', function () {
                // display error, do not login
            });
});

I assume the loginForm is the actual form, the $('form') in jQuery. 我假设loginForm是实际的形式,即jQuery中的$('form')。 Which is the same as loginSubmit in your function. 这与您的函数中的loginSubmit相同。

See, when the submit() event is raised, you call the function inside, which calls loginSubmit.submit() again. 请参见,当引发loginSubmit.submit()事件时,您在内部调用该函数,该函数再次调用loginSubmit.submit() Is it a problem? 这是个问题吗?

You can try bind the function to the submit button's click event. 您可以尝试将功能绑定到提交按钮的click事件。

Need more information about res.resume() . 需要有关res.resume()更多信息。 And can you please give us where your loginForm variable comes from? 您能给我们您的loginForm变量来自哪里吗?


UPDATE: 更新:

I would bind to the click event of the submit button. 我将绑定到“提交”按钮的click事件。

$(document).on('click','btn#submit', function(e){
    e.preventDefault();
    // make the get call
    $.get(url)
         .done(function(){
             // success callback => submit the form.
             $('form').submit();
         })
         .fail(function(){
             // fail callback => display error.
         });
});

I don't know much about your https call. 我对您的https呼叫了解不多。 And I am also not very clear about that your res.resume() . 我也不太清楚您的res.resume() But you should have some clue now. 但是您现在应该有了一些线索。

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

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