简体   繁体   中英

Trying to use jQuery .post() to redirect user after form POST

log_and_redirect.html

   <HTML>
        <HEAD>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                      $('myLoginForm').submit(function(){
                        $.post('<website_to_log_into>', function() {
                          window.location = 'http://google.com';
                        });
                        return false;
                      });
                  });
            </script>
        </HEAD>

        <BODY>
            <FORM NAME="myLoginForm" METHOD="POST" ACTION="<website_to_log_into>">
                <INPUT TYPE="hidden" NAME="username" VALUE="myUsername">
                <INPUT TYPE="hidden" NAME="password" VALUE="myPassw@rd">
            </FORM>
        </BODY>
    </HTML>

I'm trying to use the jQuery .post() to submit the form to <website_to_log_into> with the username and password, and then redirect the user to Google (while still keeping them logged in). I tried running this HTML (with the <website_to_log_into> field filled in properly), but it doesn't seem to work. When I click on log_and_redirect.html , the browser window is completely blank. I'm fairly new to javascript and jQuery, so I was just wondering if there's anything wrong that I'm doing here and how I can correct it.

Your selector is wrong. $('myLoginForm') matches an element whose tag is myLoginForm , eg <myLoginForm ...> . To match the name attribute, you have to use an attribute selector. Also, you need to provide the parameters to post.

$('form[name=myLoginForm]').submit(function() {
    $.post('<website>', $(this).serialize(), function() {
        window.location = 'http://google.com';
    });
    return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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