在我的申请表中,有时是直接提交,有时是通过 来自 jquery 插件的 .ajaxForm() 。 我想知道表格是如何提交的。我可以在 onsubmit 事件中了解这一点吗?
$(document).on('submit', 'form', function(e) {
if (e.method_to_check_is request_was_ajax)
// rest of code here
});
经过几条评论后,我必须编辑问题。
最佳答案
根据您的描述,它不会是一个ajax请求,因为您正在使用on('submit', function(){});
,它在您提交html表单时被调用,并且它将是对服务器的 html 请求。
如果您仍然想检查请求的类型是 GET 还是 POST,您可以检查表单的 method
属性。
$(document).on('submit', 'form', function(e) {
e.preventDefault();
var method = this.method;
if(method=='GET')
//do something
if(method=='POST')
//do something
});
关于javascript - 如何在 onsubmit 事件中检查请求类型(ajax 或 no)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509160/