如何在阿贾克斯成功提交表单: [英] How to submit a form on Ajax success:

查看:93
本文介绍了如何在阿贾克斯成功提交表单:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证与jQuery的投入。如果输入验证,如果阿贾克斯返回成功然后,我需要让表单提交。我是preventing提交与即preventDefault

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

我的脚本是:

$("#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);
        },
    });

成功它执行提交操作,但反复进行的操作。意味着在其控制台请求Firebug显示号码。和响应的请求数量太多。

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.

和形式是:

<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>

请谁能告诉我如何在AJAX提交表单成功 ...谢谢...

Please anyone tell me how to submit the form on ajax success ... Thanks ...

推荐答案

如果输入进行验证,那么你可以登录的用户,从同一code创建会话,并重定向到相应的页面,你的成功回调,像

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

这篇关于如何在阿贾克斯成功提交表单:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆