这段代码出了什么问题? :/

时间:2011-09-01 14:33:25

标签: javascript ajax jquery xmlhttprequest

我认为我的代码没问题,但是当我尝试启动绑定到#save输入的ajax调用时,它重新加载页面并且没有启动xhr调用:/,firebug控制台向我显示此错误:

uncaught exception: [Exception... "Not enough arguments [nsIDOMLocation.replace]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://localhost/mysite/js/jquery.js :: <TOP_LEVEL> :: line 18" data: no] http://localhost/mysite/js/jquery.js Line 18

我正在使用jQuery 1.6.2版本,我的代码就是这样:

<ul class="profile-account">
                    <li><h1>Account</h1></li>
                    <li><h4>Username</h4><input type="text" id="username" class="bradius3" value="<?php echo $populateUser->username; ?>"/></li>
                    <li class="error" id="error_username"></li>
                    <li><h4>Email</h4><input type="text" id="email" class="bradius3" value="<?php echo $populateUser->email; ?>"/></li>
                    <li class="error" id="error_email"></li>
                    <li><h4>Password</h4><input type="password" id="password" class="bradius3"/></li>
                    <li class="error" id="error_password"></li>
                    <li><h4>Password Confirm</h4><input type="password" id="confirm-password" class="bradius3"/></li>
                    <li class="error" id="error_password_confirm"></li>
                    <li><h4>Location</h4><input type="text" class="bradius3" id="location" /></li>
                    <li><h1>About Me</h1><textarea id="about"><?php echo $populateUser->about; ?></textarea></li>
                    <li class="error" id="error_about"></li>
                    <li><input type="submit" id="save" class="button floatRight bradiusMax" value="Save"></li>

                 </ul>

和ajax脚本:

var url_modify_account_xhr = "<?php echo site_url('auth/modify_account_xhr');?>";
var username;
var email;
var password;
var password_confirm;
var location;
var about;
$(document).ready(function(){
$('#users,#users span').addClass('active');
$('#myaccount').addClass('user-profile-menu-active');

$('#save').live('click',function(){
username = $('#username').val();
email = $('#email').val();
password = $('#password').val();
password_confirm = $('#password_confirm').val();
location = $('#location').val();
about = $('#about').val();
modify_account_xhr(url_modify_account_xhr,username,email,password,password_confirm,location,about);
});

});

function modify_account_xhr(url_modify_account_xhr,username,email,password,password_confirm,location,about){

   $.ajax({
        type:'POST',
        dataType:'json',
        url: url_modify_account_xhr,
        data: {'username':username,'email':email,'password':password,'password_confirm':password_confirm,'location':location,'about':about},
        beforeSend:function(){
        $('.ajax-loading').show();
       // $(_starter).attr("disabled","disabled");
        },
        success:function(json){
        $('.ajax-loading').hide();
        //$(_starter).attr("disabled","");
        }


    });
}

我真的疯了:||

3 个答案:

答案 0 :(得分:5)

不,当其中一个数据值丢失或者不是预期的格式时,它会抛出该错误。例如: 'username':username username是通过此username = $('#username').val()得到的,而val()可能是空的。在将数据传递给AJAX请求之前验证数据!

答案 1 :(得分:3)

您可以尝试将$('#save').live('click',function(){更改为:

$('#save').live('click',function(event){
    event.preventDefault();
    // function code here
});

jQuery 1.7 +的新功能:

$(document).on("click", "#save", function(event){
    event.preventDefault();
    // function code here
});

答案 2 :(得分:1)

我解决了我认为这是一个FF问题,我现在刷新很多次似乎没问题:/我过了半天这个愚蠢的错误:////谢谢任何人