简体   繁体   中英

Login to a site without redirect to that site using jQuery/ajax

I want to send a login form to a site without having the page redirect to that site but rather just display a blank page instead. I have been looking around and noticed jquery would help me with this but I haven't found a way to get it to work quite right so I was hoping for some advice. This is what I have right now.

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
    <form id="myForm" action="placeholderurl" method="post"> 
        <input type="hidden" name="username" value = "placeholder"/> 
        <input type ="hidden" name="password" value = "placeholder"/>
        <input type="submit"/> 
    </form>
<script>
    $(document).ready(function() {
    $('#myForm').submit(function(e) {
    e.preventDefault();
    var formdata = $('#myForm').serialize();
     $.ajax({
     url: "placeholderurl",
     type: "POST",
     data: formdata,
     cache: false,
     success: function(data) {
        alert("yeah");
        //?code to display blank page after successful login??
     },
     error: function(){
        alert("noo");
     }
     });
});
});
 </script> 
 </head>
</html>

Currently, the code always goes into the "noo" error block. I'm not sure how to extract more information out of the error so I don't know exactly what is going wrong. Any advice/tips would be appreciated.

*Edit The placeholderurl and placeholder are filled in with the correct information in my actual code. Also, the url I want to post to is not in the same domain as the function is being called from so ajax may not work for this(comment from Archer). Since this is the case, is there another way to get the desired behavior that I can try without using ajax. Thanks again.

I'd suggest watching your network traffic in something like Fiddler, Firebug, or Chrome's developer tools and see what the response is that is causing the error. I'm guessing your placeholderurl is on a different domain and your call is failing due to that.

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