javascript - 这段代码有什么问题? :/

标签 javascript ajax jquery xmlhttprequest

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

uncaught exception: [Exception... "Not enough arguments [nsIDOMLocation.replace]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: <a href="http://localhost/mysite/js/jquery.js" rel="noreferrer noopener nofollow">http://localhost/mysite/js/jquery.js</a> :: <TOP_LEVEL> :: line 18" data: no] <a href="http://localhost/mysite/js/jquery.js" rel="noreferrer noopener nofollow">http://localhost/mysite/js/jquery.js</a> 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","");
        }


    });
}

我真的要疯了:||

最佳答案

不,当其中一个数据值丢失或不符合预期格式时,它会抛出该错误。例如: '用户名':用户名 username 是通过以下方式获取的:username = $('#username').val() 并且 val() 可能为空。在将数据传递给 AJAX 请求之前验证数据!

关于javascript - 这段代码有什么问题? :/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7271870/

相关文章:

Javascript:setAttribute() 与 setAttribute() 比较element.attribute = 设置 "name"属性的值

javascript - 为什么 +new Date() 返回的结果与 new Date().getTime() 相同?

javascript - jQuery 响应式菜单删除 CSS 背景

javascript - 识别哪个选项卡发出了请求

javascript - 无限滚动显示重复结果

c# - 带有详细信息表的 JQuery 网格

javascript - react : Grabbing properties from child components - how?

ajax - 我可以使用 hashbang (#!) URL 向 ajax 驱动的网站添加自定义标题吗?

javascript - 我的 jQuery Ajax 无法与 PHP 配合使用

javascript - 使用 jQuery 将所有事件从一个元素传播到另一个元素