jquery POST作为GET发送

时间:2015-05-20 20:46:15

标签: jquery ajax

代码正在运行,但它正在URL中作为GET发送。

我在这里看过其他问题,但似乎没有相关内容。

$("#securityInfoUpdate").click(function() {

        var newUserName = $('#securityInfoUserName').find(":input").val();
        var newPassword = $('#securityInfoPassword').find(":input").val();
    $.post( "ajaxProcessing.php", { username: newUserName, password:newPassword } )
                .done(function( data ) {
                var success = jQuery.parseJSON(data);
                if (success == 1){          
                    $("#securityInfoMessage").html('YES!! DATABASE CHANGED');

                }else if(success == 2){
                    $("#securityInfoMessage").html('PROBLEM BUDDY!');
                }

            });
});

它还会导致页面刷新。这可能是什么原因?

如果需要或相关其他信息,请与我们联系。

HTML

<form id= "securityInfoValuePairs">
                        Change username and password:<br/>
                        <div id="securityInfoMessage">
                        </div>
                        <div id="securityInfoUserName">
                        </div>
                        <div id="securityInfoPassword">
                            <label for="passwordLate">NEW or OLD Password:</label>
                            <input type="text" name="passwordLate" placeholder="PLEASE ENTER A PASSWORD">
                        </div>
                        <button id="securityInfoUpdate">UPDATE</button>
                    </form>

表单中的BTW信息和输入字段由js通过loalStorage提供

2 个答案:

答案 0 :(得分:4)

button的默认类型是提交,form的默认方法是GET,因此当您单击按钮时,由于它没有指定类型且表单没有指定方法,并且您没有使用任何JavaScript来阻止默认行为,它会提交表单并重新加载页面。

一个简单的解决方法是使用return false.preventDefault()来阻止按钮在发出AJAX请求时提交表单。您可以查看this question以了解方法之间的差异。

答案 1 :(得分:2)

return false;添加到click功能的末尾:

$("#securityInfoUpdate").click(function() { 
    ....

    return false;
});