简体   繁体   English

如何提交有关Ajax成功的表单:

[英]How to submit a form on Ajax success:

I'm trying to validate the inputs with jQuery. 我正在尝试使用jQuery验证输入。 If the inputs are validated and if ajax returns success Then I need to allow the form to submit. 如果输入被验证并且ajax返回success那么我需要允许表单提交。 I'm preventing the submit with e.preventDefault 我正在使用e.preventDefault阻止提交

My Script is : 我的剧本是:

$("#spsignin").submit(function(e){
    if (e.preventDefault) { 
        e.preventDefault(); 
    } else { 
        e.returnValue = false;
    }
    var uname = $("input#name").val();
    var pass = $("input#password").val();

    $.ajax({
        type: "POST",
        url: "Login",
        data: 'uname=' + encodeURIComponent(uname) + '&' + 'pass=' 
                       + encodeURIComponent(pass),
        dataType: "json",
        success: function( data, textStatus, jqXHR) {
            if (data == true) {
                $("#spsignin").submit();
            } else { //display error message
                $("#error").show(1500).css({visibility: "visible"});
                $("#error").append("<b>Invalid Username/Email or Password</b>");
            }
        },
        //If there was no resonse from the server
        error: function(jqXHR, textStatus, errorThrown) {
            console.log("Something really bad happened " + textStatus);
            $("#error").html(jqXHR.responseText);
        },
    });

On success it performs submit action but it performing the action repeatedly. success它会执行提交操作,但会重复执行操作。 Means Firebug showing number of post requests in its console. 表示Firebug在其控制台中显示多个post请求。 And number of post requests in response too. 并且响应的post请求数也是如此。

And form is: 形式是:

<form action="Signin" method="get" id="spsignin">
    <input type="text" name="uname" class="text validate[required]" 
        id="name" placeholder="Username"/>
    <input type="password" name="pass" class="text validate[required]" 
        id="password" placeholder="Password"/>
    <input type="submit" value="" id="memberlogin"/>
</form>

Please anyone tell me how to submit the form on ajax success ... Thanks ... 请有人告诉我如何提交关于ajax success的表格...谢谢......

If the inputs are validated, then you could log the user in, create session from the same code and redirect to appropriate page in your success callback, like 如果输入已经过验证,那么您可以将用户登录,从相同的代码创建会话并重定向到成功回调中的相应页面,如

...
success: function( data, textStatus, jqXHR) {
  if(data==true) {
   //redirect to loggedin page
   location.href = "url_to_your_loggedin_page";               
  }

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

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