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

时间:2021-03-25 20:32:46

I'm trying to validate the inputs with jQuery. If the inputs are validated and if ajax returns success Then I need to allow the form to submit. I'm preventing the submit with e.preventDefault

我正在尝试用jQuery验证输入。如果输入被验证,如果ajax返回成功,那么我需要允许表单提交。我用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. Means Firebug showing number of post requests in its console. And number of post requests in response too.

在成功上,它执行提交动作,但它会重复执行动作。表示在其控制台中显示post请求数量的Firebug。以及响应的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的表单……谢谢……

1 个解决方案

#1


3  

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";               
  }

#1


3  

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";               
  }