1

我有一个 ajax 调用,如下所示:

$("#loginForm").submit(function(e){
           $.ajax({
                type:'POST',
                url:'login.php',
                data: $("#loginForm").serialize();
                success: function(data){


                        if(data=="1"){
                        $("#message").html('<span class="gmessage">You are already logged in!</span>');
                        }
                        else if(data=="2"){
                            $("#message").html('<span class="gmessage">You have been logged in!</span><br/>'
                                               '<span class="bmessage">You will be redirected in 10 seconds</span>');
                            $("#message").fadeOut(1000, function(){
                                window.location = <?php echo $_SESSION['ref'];?>;
                            });
                        }
                        else if(data=="3"){
                             $("#message").html('<span class="rmessage">The password you entered was incorrect</span>');
                        }
                        else if(data=="4"){
                             $("#message").html('<span class="rmessage">That account does not exist, you may need to register for an account</span>');
                        }
                        else{
                            $("#message").html('<span class="rmessage">There was an error: please try again</span>');
                        }

                }

           });
           e.preventDefault();
        });

php 已经过测试,并且根据跨度中的消息按预期运行,但是当执行此 ajax 调用时,不会显示任何消息,那就是它是否正在执行。对应的html是这样的:

       <tr>
         <td id="message" name="message" colspan="2"></td>
       </tr>
        <tr>
         <td colspan="2" style="height:50px">
           <center>
                <input type="submit" name="submit" id="submit" value="Login!" style="font-family: arial;font-size:14pt;"/><br/><br/>
                <span class="registerSpan">Don't have an account?:</span><br/><br/>
                <input type="button" onClick="parent.location='createacct.php'" style="font-family:arial; font-size:14pt;" value="Register"/>
          </center>
         </td>
        </tr>

为什么这不起作用?另一个相关问题:ajax 是处理这个问题的最佳方式吗?

4

2 回答 2

2

除了@Alexander 指出的明显语法错误之外,您还应该通过dataType: "text"告诉 jQuery 期望什么数据。

此外,如果您使用带有 HTTP 状态代码的 json 会更好。成功功能可能看起来像

success(function(result) {
    var message = '<span class="gmessage">Some Initial text</span>';
    if(result.status == 200) {
        message = '<span class="gmessage">' + result.message + '</span>';
    }
    else if (result.status == 401) {
        .......
    }
    $('#message').html(message);
}

显然有一些方法可以通过使用模板来进一步优化这一点,而不是进行大量的 DOM 操作。

于 2012-09-07T13:49:49.343 回答
0

使用jsfiddle 的jslint 检查器,我对代码中存在的一些语法错误进行了排序。

$("#loginForm").submit(function(e){
       $.ajax({
            type:'POST',
            url:'login.php',
            data: $("#loginForm").serialize(),
            success: function(data){


                    if(data=="1"){
                    $("#message").html('<span class="gmessage">You are already logged in!</span>');
                    }
                    else if(data=="2"){
                        $("#message").html('<span class="gmessage">You have been logged in!</span><br/><span class="bmessage">You will be redirected in 10 seconds</span>');
                        $("#message").fadeOut(1000, function(){
                            window.location = <?php echo $_SESSION['ref'];?>;
                        });
                    }
                    else if(data=="3"){
                         $("#message").html('<span class="rmessage">The password you entered was incorrect</span>');
                    }
                    else if(data=="4"){
                         $("#message").html('<span class="rmessage">That account does not exist, you may need to register for an account</span>');
                    }
                    else{
                        $("#message").html('<span class="rmessage">There was an error: please try again</span>');
                    }

            }

       });
       e.preventDefault();
    });​
于 2012-09-07T13:03:12.643 回答