简体   繁体   English

使用jQuery / ajax登录到站点而无需重定向到该站点

[英]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. 我一直在四处看看,注意到jquery可以帮助我解决这个问题,但是我还没有找到一种使它正常工作的方法,因此我希望能提供一些建议。 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. 当前,代码始终进入“ noo”错误块。 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. *编辑在我的实际代码中,placeholderurl和占位符已填写正确的信息。 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). 另外,我要发布的url与正在从中调用该函数的域不在同一域中,因此ajax可能对此不起作用(来自Archer的评论)。 Since this is the case, is there another way to get the desired behavior that I can try without using ajax. 既然是这种情况,是否还有另一种方法可以得到所需的行为,而我无需使用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. 建议您使用Fiddler,Firebug或Chrome的开发人员工具观察您的网络流量,并查看引起该错误的响应是什么。 I'm guessing your placeholderurl is on a different domain and your call is failing due to that. 我猜您的placeholderurl在另一个域上,因此您的呼叫失败。

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

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