0

嗨,我正在使用 jquery 表单帖子。当我发布表单时,它在 IE 中运行良好,但不适用于 Chrome。我把代码放在这里:

$(document).ready(function(){
    $('#signup').submit(function(){

        var username    = $('#username').val();
        var password    = $('#password').val();
        var cpassword   = $('#cpassword').val();
        var email       = $('#email').val();
        if(password != cpassword){
            alert("Passwords are not equal");
            $('#cpassword').css('border','1px solid red');
        }else{
             $.ajax({
                type    :'post',

                url     :'http://localhost/tmc/includes/ajax-signup.php',

                data    :({password:password,username:username,email:email}),

                success : function(msg){
                    alert(msg);

                },                  
            });
        }
        //$("#submit").hide();
    });
});
4

2 回答 2

0

添加id='submit'到您的提交按钮并尝试这样的调用:

    $('#submit').click(function(e){

        e.preventDefault();

        var username    = $('#username').val();
        var password    = $('#password').val();
        var cpassword   = $('#cpassword').val();
        var email       = $('#email').val();
        if(password != cpassword){
            alert("Passwords are not equal");
            $('#cpassword').css('border','1px solid red');
        }else{
             $.ajax({
                type    :'POST',
                url     :'http://localhost/tmc/includes/ajax-signup.php',
                data    : {password:password,username:username,email:email},
                success : function(msg){
                    alert(msg);
                },                  
            });
        }
        //$("#submit").hide();
    });
于 2013-04-05T08:58:47.897 回答
0

我认为你需要这个:

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
});

只需立即进行 ajax 调用,您将在遇到任何错误时收到相应的警报!此外,使用它还有一个优点。您不需要error再次为任何 ajax 调用编写方法。它使用ajaxSetup()充当全局 ajax 错误处理程序

另外,我还想提一提的是,只需删除commaajax 调用中的额外内容。

$.ajax({
    type: 'post',
    url: 'http://localhost/tmc/includes/ajax-signup.php',
    data: ({
        password: password,
        username: username,
        email: email
    }),
    success: function (msg) {
        alert(msg);
    } //,  <---- remove this extra comma, no need of it
});
于 2013-04-05T08:53:31.480 回答