Javascript ajax调用后如何停止刷新页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27759380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop refreshing page after ajax call?
提问by Pradeepb
I am unable to stop refreshing page after ajax call. I have tried by putting e.preventDefault(); and return false; as well but again my page is refreshing.
ajax 调用后我无法停止刷新页面。我试过把 e.preventDefault(); 并返回假;同样,但我的页面再次令人耳目一新。
I dont know what is the problem with the code or something. Please help me to stop refreshing page after ajax call. solving this issue would be a great help for me. Thanks in advance.
我不知道代码有什么问题还是什么。请帮助我在 ajax 调用后停止刷新页面。解决这个问题对我来说是一个很大的帮助。提前致谢。
Here is my code:
这是我的代码:
$(document).ready(function() {
$('#loginForm').on('click', function(e) {
e.preventDefault();
var formData = {
'uname' : $('#uname').val(),
'pwd' : $('#pwd').val()
};
$.ajax({
type : "POST",
url : "getresults.php",
data : formData
}).done(function(data) {
alert(data+"This is working");
}).fail(function(data) {
alert("This is not working");
});
});
});
采纳答案by Sebastian Bork
Is the id #loginFormpointing to a form? If yes you need to listen to the submit event instead of click. If you really need to listen to the click event, you have to bind the event to the submit button or whatever triggers the form.submit().
id#loginForm指向一个表单吗?如果是,您需要收听提交事件而不是单击。如果你真的需要监听点击事件,你必须将事件绑定到提交按钮或任何触发form.submit().
Try something like this:
尝试这样的事情:
$('#loginForm').on('submit', function(e) {
e.preventDefault();
e.stopPropagation(); // only neccessary if something above is listening to the (default-)event too
[YOUR CODE GOES HERE]
});
This should do the trick for you.
这应该对你有用。
回答by isidat
Adding type="button"attribute to button solved my problem. Otherwise it was interpreted as submit operation.
向type="button"按钮添加属性解决了我的问题。否则它被解释为提交操作。
回答by deenbandhu
I think just adding return false after the on click function will stop the page from refreshing
我认为在点击功能后添加 return false 会阻止页面刷新
回答by fanfare
This was happening to me earlier tonight, and I found my way to this question - This might be a long shot but I realized what my issue was and figure this might help someone in the future dealing with this.
今晚早些时候这发生在我身上,我找到了解决这个问题的方法 - 这可能是一个长期的问题,但我意识到我的问题是什么,并认为这可能会帮助将来有人处理这个问题。
Are you using something like live-serverlocally or some other form of script that intelligently refreshes local code if new files appear in the same working folder? If so - the page is refreshing not because of your code, but because whatever ajax you called manipulated the folder you were working in resulting in the page 'updating'.
live-server如果新文件出现在同一工作文件夹中,您是否使用本地或其他形式的脚本智能刷新本地代码?如果是这样 - 页面刷新不是因为您的代码,而是因为您调用的任何 ajax 操作了您正在处理的文件夹,导致页面“更新”。
回答by Hareendran MG
This can happen when you have turned on live serverextension or something like that refresh html pages.
当您打开实时服务器扩展或类似刷新 html 页面的内容时,可能会发生这种情况。

